在silverlight中怎么动态改变style中的值
样式代码如下[code=XML]<Stylex:Key="Style2"TargetType="Primitives:DataGridColumnHeader"><Set...
样式代码如下
[code=XML]
<Style x:Key="Style2" TargetType="Primitives:DataGridColumnHeader">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Primitives:DataGridColumnHeader">
[color=#FF0000]<Border Name="aaa" Background="Gold" Width="50">[/color]
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
[/code]
我想在后台c#代码中改变ControlTemplate 中名为'aaa'的border的width的值,应该怎么弄?
我自己写了一段;
[code=C#]
public static DependencyProperty BorderProperty = DependencyProperty.Register("Border", typeof(Border), typeof(MainPage), null);
Style style = this.Resources["Style2"] as Style;
SetterBaseCollection setterBaseCollection = style.Setters;
Setter setter = setterBaseCollection[2] as Setter;
ControlTemplate controlTemplate = setter.Value as ControlTemplate;
object c = controlTemplate.GetValue(BorderProperty );
[/code]
但是在获取controlTemplate.GetValue(BorderProperty )时老是取到null,BorderProperty 应该怎么写,这种方法可以改变aaa的width的值,或者还有另外的方法?
谢谢 展开
[code=XML]
<Style x:Key="Style2" TargetType="Primitives:DataGridColumnHeader">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Primitives:DataGridColumnHeader">
[color=#FF0000]<Border Name="aaa" Background="Gold" Width="50">[/color]
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
[/code]
我想在后台c#代码中改变ControlTemplate 中名为'aaa'的border的width的值,应该怎么弄?
我自己写了一段;
[code=C#]
public static DependencyProperty BorderProperty = DependencyProperty.Register("Border", typeof(Border), typeof(MainPage), null);
Style style = this.Resources["Style2"] as Style;
SetterBaseCollection setterBaseCollection = style.Setters;
Setter setter = setterBaseCollection[2] as Setter;
ControlTemplate controlTemplate = setter.Value as ControlTemplate;
object c = controlTemplate.GetValue(BorderProperty );
[/code]
但是在获取controlTemplate.GetValue(BorderProperty )时老是取到null,BorderProperty 应该怎么写,这种方法可以改变aaa的width的值,或者还有另外的方法?
谢谢 展开
1个回答
展开全部
这样做太复杂,我个人建议这样处理:
修改border的HorizontalAlignment="Stretch"
然后在Style里设置宽度属性 <Setter Property="Width" Value="50"/>
接着再做一个基于前者的Style
<Style TargetType="Primitives:DataGridColumnHeader" x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
<Setter Property="Width" Value="60"/>
</Style>
最后在需要切换的程序里加
DataGridColumnHeader.Style = Application.Current.Resources["StyleTwo"] as Style;
修改border的HorizontalAlignment="Stretch"
然后在Style里设置宽度属性 <Setter Property="Width" Value="50"/>
接着再做一个基于前者的Style
<Style TargetType="Primitives:DataGridColumnHeader" x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
<Setter Property="Width" Value="60"/>
</Style>
最后在需要切换的程序里加
DataGridColumnHeader.Style = Application.Current.Resources["StyleTwo"] as Style;
更多追问追答
追问
因为我的页面需要在不同的屏幕分辨率下显示,需要做成自适应的控件,那应该怎么弄呢,谢谢
Style style = this.Resources["Style2"] as Style;
SetterBaseCollection setterBaseCollection = style.Setters;
Setter setter = setterBaseCollection[2] as Setter;
//setter.Value = 20.0;//这里报错,说没有权限操作.
是不是silverlight的style中的值不允许改变
追答
如果要做成自适应的控件,根本不需要设置高度和宽度啊。只需要设置控件的HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 就可以实现高度自适应和宽度自适应。再加上其父容器Grid的行高列宽为自适应(*)即可。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询