wpf 如何动态的设置Grid宽和高.
在窗体加载事,我new出几个ColumnDefinition和RowDefinition,如何去设置他们的高和宽?或者GridLength怎么用,我想设置行和列的具体像素...
在窗体加载事,我new出几个ColumnDefinition和RowDefinition,如何去设置他们的高和宽?或者GridLength怎么用,我想设置行和列的具体像素。该如何做?
展开
2个回答
推荐于2016-10-06 · 知道合伙人教育行家
关注
展开全部
WPF动态改变grid行宽或者列高,需要创建GridLength的动画类。
(一) 创建一个支持GridLength类型的动画类
新建一个继承AnimationTimeLine的类GridLengthAnimation, 简单实现2个依赖属性"From", "To".代码如下:
internal class GridLengthAnimation : AnimationTimeline
{
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
}
public static readonly DependencyProperty FromProperty;
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
接下来就来依次重载或者实现AnimationTimeLine类的成员,
1. 重载CreateInstanceCore, 代码如下:
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
2. 重载GetCurrentValue以返回动画的当前值, 代码如下:
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
if (fromVal > toVal)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal,
((GridLength)GetValue(GridLengthAnimation.FromProperty)).GridUnitType);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal,
((GridLength)GetValue(GridLengthAnimation.ToProperty)).GridUnitType);
}
3. 重写TargetPropertyType 属性以指示相应的动画所生成输出的Type, 代码如下:
public override Type TargetPropertyType
{
get
{
return typeof(GridLength);
}
}
ok, 通过上面的步骤我们已经写好了GridLengthAnimation类, 接下来就是如何使用此类.
(二)xaml使用此类, 代码如下:
<Window.Resources>
<Storyboard x:Key="sbDock">
<common:GridLengthAnimation BeginTime="00:00:00" Storyboard.TargetName="_cellLeft" Storyboard.TargetProperty="Width">
</common:GridLengthAnimation>
</Storyboard>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="_cellLeft" Width="300"/>
<ColumnDefinition x:Name="_cellRight" Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
(三)c#使用此类, 代码如下:
Storyboard sbDock = this.FindResource("sbDock") as Storyboard;
if (sbDock != null)
{
SplineDoubleKeyFrame sdKeyFrame1 = new SplineDoubleKeyFrame(TransformRadius,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)));
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Clear();
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Add(sdKeyFrame1);
(sbDock.Children[1] as GridLengthAnimation).From = new GridLength(300, GridUnitType.Pixel);
(sbDock.Children[1] as GridLengthAnimation).To = new GridLength(0, GridUnitType.Pixel);
sbDock.Begin();
}
(一) 创建一个支持GridLength类型的动画类
新建一个继承AnimationTimeLine的类GridLengthAnimation, 简单实现2个依赖属性"From", "To".代码如下:
internal class GridLengthAnimation : AnimationTimeline
{
static GridLengthAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(GridLength),
typeof(GridLengthAnimation));
ToProperty = DependencyProperty.Register("To", typeof(GridLength),
typeof(GridLengthAnimation));
}
public static readonly DependencyProperty FromProperty;
public GridLength From
{
get
{
return (GridLength)GetValue(GridLengthAnimation.FromProperty);
}
set
{
SetValue(GridLengthAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public GridLength To
{
get
{
return (GridLength)GetValue(GridLengthAnimation.ToProperty);
}
set
{
SetValue(GridLengthAnimation.ToProperty, value);
}
}
接下来就来依次重载或者实现AnimationTimeLine类的成员,
1. 重载CreateInstanceCore, 代码如下:
protected override System.Windows.Freezable CreateInstanceCore()
{
return new GridLengthAnimation();
}
2. 重载GetCurrentValue以返回动画的当前值, 代码如下:
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;
if (fromVal > toVal)
{
return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal,
((GridLength)GetValue(GridLengthAnimation.FromProperty)).GridUnitType);
}
else
return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal,
((GridLength)GetValue(GridLengthAnimation.ToProperty)).GridUnitType);
}
3. 重写TargetPropertyType 属性以指示相应的动画所生成输出的Type, 代码如下:
public override Type TargetPropertyType
{
get
{
return typeof(GridLength);
}
}
ok, 通过上面的步骤我们已经写好了GridLengthAnimation类, 接下来就是如何使用此类.
(二)xaml使用此类, 代码如下:
<Window.Resources>
<Storyboard x:Key="sbDock">
<common:GridLengthAnimation BeginTime="00:00:00" Storyboard.TargetName="_cellLeft" Storyboard.TargetProperty="Width">
</common:GridLengthAnimation>
</Storyboard>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="_cellLeft" Width="300"/>
<ColumnDefinition x:Name="_cellRight" Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
(三)c#使用此类, 代码如下:
Storyboard sbDock = this.FindResource("sbDock") as Storyboard;
if (sbDock != null)
{
SplineDoubleKeyFrame sdKeyFrame1 = new SplineDoubleKeyFrame(TransformRadius,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)));
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Clear();
(sbDock.Children[0] as DoubleAnimationUsingKeyFrames).KeyFrames.Add(sdKeyFrame1);
(sbDock.Children[1] as GridLengthAnimation).From = new GridLength(300, GridUnitType.Pixel);
(sbDock.Children[1] as GridLengthAnimation).To = new GridLength(0, GridUnitType.Pixel);
sbDock.Begin();
}
展开全部
后台代码指定第 i 行的高:
this.grid.RowDefinitions[i].Height = new GridLength(int, GridUnitType.Pixel);
// GridUnitType.Pixel 表示像素。
希望对你有帮助。
this.grid.RowDefinitions[i].Height = new GridLength(int, GridUnitType.Pixel);
// GridUnitType.Pixel 表示像素。
希望对你有帮助。
追问
很久都没来看了。 如果是动态加载Label或者Border其他的控件,我想指定它的Height和Width怎么去写呢?
追答
控件的Height和Width是Double类型,直接赋值就行,例如
Border bd= new Border();
bd.Height = 300.0;
bd.Width = 500.0;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询