日期:2014-05-18 浏览次数:20902 次
<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; } } }