数据触发器为啥不好用啊?

Joanna_yan 2012-05-28 06:12:15
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=St}" Value="2">
<Setter Property="Template" Value="{StaticResource Eating}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="Empty" TargetType="{x:Type Button}">
<Grid >
<Image x:Name="myimage" Source="/WpfApplication4;component/Images/2.jpg" Height="200" Width="200" />
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="Eating" TargetType="{x:Type Button}">
<Grid >
<Image x:Name="myimage" Source="/WpfApplication4;component/Images/1.jpg" Height="200" Width="200" />
</Grid>
</ControlTemplate>

</Window.Resources>
<Grid>
<Button Template="{StaticResource Empty}" Height="200" Margin="146,38,157,73" Name="button1" Width="200" />
</Grid>
</Window>



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new D("2");
}
class D
{
string _St;
public string St
{
get { return _St; }
set { _St = value; }
}
string _k;
public string k
{
get { return _k; }
set { _k = value; }
}
public D(string name, string age)
{
_St = St;
_k = k;
}
}
}


k先不用,但是St=2时Template没有从Empty变为Eating。

谁能帮这改一下~
...全文
65 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
candy2688 2012-06-02
  • 打赏
  • 举报
回复
<Setter Property="Template" Value="{StaticResource Eating}"/>
这个改为
<DataTrigger Binding="{Binding Path=St}" Value="2">
<Setter TargetName="rectangle" Property="Fill">
<Setter.Value>
<ImageBrush ImageSource="/WpfApplication4;component/Images/1.jpg" ></ImageBrush>
</Setter.Value>
</Setter>
</DataTrigger>

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧