将按钮的tag属性设置为整数值并触发触发器

时间:2011-09-16 13:21:31

标签: wpf xaml datatemplate datatrigger setter

我想做类似以下的事情,但它不起作用。我想使用datatrigger将一个整数(或任何类型)值设置为我在datatemplate中定义的按钮的Tag属性。然后在按钮的图像样式内部,当相应的值设置为按钮的tag属性时触发一个触发器。但它似乎并没有这样做。如果我设置x:Null值,我可以触发触发器,这意味着逻辑正常。但设置0或1之类的值不会触发触发器。任何解决方案?

<DataTemplate x:Key="SomeDataTemplateofSomeType">

<Button x:Name="ButtonVisible">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Image x:Name="FxImage">
                  <Image.Style>
                      <Style TargetType="{x:Type Image}">
                        <Style.Triggers>
                           <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Value="0">
                               <Setter Property="Source" Value="/Resources/controls/images/fxiconsnone.png" />
                           </DataTrigger>
                        </Style.Triggers>
                      </Style>
                 </Image.Style>
            </Image>
        </ControlTemplate>
   </Button.Template>
</Button>

<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsVisible, Value="True">
    <Setter Property="Tag" TargetName="ButtonVisible" Value="1" />
</DataTrigger>

<DataTrigger Binding="{Binding Path=IsVisible, Value="False">
    <Setter Property="Tag" TargetName="ButtonVisible" Value="0" />
</DataTrigger>
</DataTemplate.Triggers>
</Datatemplate>

1 个答案:

答案 0 :(得分:2)

您绑定到DataContext的{​​{1}},而不绑定到模板Image

绑定到Button

另一方面,没有必要使用两个TemplatedParent来表示true和false。这样做

DataTriggers

只保留其中一个<Button x:Name="ButtonVisible" Tag="1">

DataTriggers

示例,将以下内容添加到<DataTrigger Binding="{Binding Path=IsVisible}" Value="False"> <Setter Property="Tag" TargetName="ButtonVisible" Value="0" /> </DataTrigger>

MainWindow.xaml

<强> MainWindow.xaml.cs

<StackPanel>
    <StackPanel.Resources>
        <DataTemplate x:Key="SomeDataTemplateofSomeType">
            <Button x:Name="ButtonVisible">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Image x:Name="FxImage">
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Value="0">
                                            <Setter Property="Source" Value="YourImageSource" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsVisible}" Value="True">
                    <Setter Property="Tag" TargetName="ButtonVisible" Value="1" />
                </DataTrigger> 
                <DataTrigger Binding="{Binding Path=IsVisible}" Value="False">
                    <Setter Property="Tag" TargetName="ButtonVisible" Value="0" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </StackPanel.Resources>
    <ContentControl Content="{Binding MySource}"
                    ContentTemplate="{StaticResource SomeDataTemplateofSomeType}"/>
    <ToggleButton IsChecked="{Binding Path=MySource.IsVisible, Mode=TwoWay}" Content="IsChecked?"/>
</StackPanel>