WPF 查找button中style属性中image
<ButtonHorizontalAlignment="Left"Margin="871,11,0,0"Name="button1"VerticalAlignment="...
<Button HorizontalAlignment="Left" Margin="871,11,0,0" Name="button1" VerticalAlignment="Top" BorderThickness="0" Cursor="Hand" Visibility="Visible" Click="button1_Click">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Image Name="coles" Stretch="None" Source="/FiscalEncyclopedia;component/Images/close_03.png"></Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button
我想后台获取button中这个Image的Source属性 展开
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Image Name="coles" Stretch="None" Source="/FiscalEncyclopedia;component/Images/close_03.png"></Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button
我想后台获取button中这个Image的Source属性 展开
2个回答
展开全部
var s = this.button1.Style.Setters[0] as Setter;
var t = s.Value as ControlTemplate;
var g = t.LoadContent() as Grid;
var b = g.Children[0] as Image;
var source = b.Source;
最后的source 就是你要的值。
上面的方式是创建的新实例。如果你要修改button的值。可以用下面的方式。
var t = this.button1.GetValue(TemplateProperty) as ControlTemplate;
var b = t.FindName("coles", this.button1) as Image;
b.Source = null;//可以修改这个值
var t = s.Value as ControlTemplate;
var g = t.LoadContent() as Grid;
var b = g.Children[0] as Image;
var source = b.Source;
最后的source 就是你要的值。
上面的方式是创建的新实例。如果你要修改button的值。可以用下面的方式。
var t = this.button1.GetValue(TemplateProperty) as ControlTemplate;
var b = t.FindName("coles", this.button1) as Image;
b.Source = null;//可以修改这个值
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询