日期:2014-05-18  浏览次数:20882 次

数据触发器为啥不好用啊?
C# code
<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>



C# code

    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。

谁能帮这改一下~

------解决方案--------------------
<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>