¿Cómo establecer dinámicamente el ancho y el alto de la cuadrícula en WPF?
(1) Cree una clase de animación que admita el tipo GridLength.
Cree una nueva clase GridLengthAnimation que herede AnimationTimeLine y simplemente implemente dos propiedades de dependencia "From", "to" y "To". El código es el siguiente:
Clase interna GridLengthAnimation: línea de tiempo de animación
{
Static GridLengthAnimation()
{
de propiedad = propiedad de dependencia. Register("From ", tipo de(GridLength),
tipo de(GridLengthAnimation)
ToProperty = DependencyProperty. Register("To ", typeof(GridLength),
type of(GridLengthAnimation));
}
Propiedad de dependencia pública estática de solo lectura de la propiedad p> p>
La grilla pública se obtiene de
{
{
return(GridLength)GetValue(GridLengthAnimation .from propiedad) ;
}
Establecer
{
SetValue(GridLengthAnimation.FromProperty, valor);
}
}
dependencia pública estática de solo lectura de propiedad a propiedad;
La red eléctrica pública se extiende a
{
Obtener
{
return(GridLength)GetValue(GridLengthAnimation.to propiedad);
}
Configuración p>
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
A continuación, sobrecargaremos o implementar AnimationTimeLine a su vez miembros de la clase.
1. Sobrecargue CreateInstanceCore, el código es el siguiente:
Sistema de anulación protegido. windows . freez able CreateInstanceCore()
{
Devuelve new GridLengthAnimation();
}
2. Valor actual de la animación.
El código es el siguiente:
Objeto de anulación pública GetCurrentValue (objeto defaultOriginValue,
Objeto defaultDestinationValue, reloj de animación reloj de animación)
{
doble de val =((GridLength)GetValue(GridLengthAnimation.FromProperty)). Valor;
doble a val =((GridLength)GetValue(GridLengthAnimation.ToProperty)). valor;
if(from val gt; tovar)
{
Devuelve la nueva longitud de la cuadrícula ((1 - AnimationClock. Progreso actual. Valor) *( from val-toVal) toVal,
((GridLength)GetValue(GridLengthAnimation.FromProperty)).GridUnitType);
}
Otros
Devuelve nuevo GridLength(animationClock. progreso actual. Valor *(toVal-from val) de val
((GridLength)GetValue(GridLengthAnimation.ToProperty)).GridUnitType);
}
3. Anule la propiedad TargetPropertyType para indicar el tipo de salida generado por la animación correspondiente. El código es el siguiente:
Tipo público anulado TargetPropertyType
{
Get
{
Tipo de retorno of( GridLength);
}
}
Bien, a través de los pasos anteriores hemos escrito la clase GridLengthAnimation y el siguiente paso es cómo usarla. .
(2) xaml usa esta clase, el código es el siguiente:
lt window. Recursos gt
lt Storyboard x: Clave = " sb dock " gt;
lt Común: Hora de inicio de GridLengthAnimation = " 00:00:00 " Storyboard. TargetName="_cellLeft "Guión gráfico. TargetProperty = " Ancho " gt
lt/common: GridLengthAnimation gt;
lt/story board gt
lt/window. Recursos gt
ltgrid x: Nombre = "raíz de diseño" Fondo = "Blanco" gt;
ltgrid. RowDefinitions gt
ltRowDefinition/>
lt/grid. Definiciones de fila gt
ltGrid.
Definiciones de columna gt
definición de columna x: Nombre = " _ celda izquierda " Ancho = " 300 "/ gt
definición de columna x: Nombre = " _ celda derecha " Ancho = " * "/gt;
lt/grid. ColumnDefinitions gt
lt/Grid gt;
(3) c# usa esta clase, el código es el siguiente:
Storyboard sbDock = this. buscar recurso("sb dock") como guión gráfico;
if (sbDock!=null)
{
fotograma splinedoublekey fotograma clave SD 1 = nuevo fotograma splinedoublekey (radio de transformación,
Momentos clave. FromTimeSpan(TimeSpan. from segundos(1));
(sbDock. Children[0] as doubleanimationusingkey frames). key frames . clear() ;
(sbDock. Children[0] como animación doble usando fotogramas clave). add (fotograma clave SD 1);
(sbDock. Children[1] como GridLengthAnimation). = new GridLength(300, GridUnitType. pixels);
(sbDock. Children[1] as GridLengthAnimation = new GridUnitType. pixels
sbDock . comenzar();
}