“标签”...... WPF中的特殊功能?

时间:2011-08-04 12:49:47

标签: wpf xaml

MSDN说“获取或设置一个可用于存储有关此元素的自定义信息的任意对象值。”这意味着我可以在这个属性中存储任何我想要的东西。

但是如果你绑定到这个属性(类型为String的属性,其值为“XYZ”)并在Trigger条件下使用它就不起作用了!

  <Trigger Property="Tag" Value="XYZ">
      <Setter Property="Background" Value="Red" />
  </Trigger>

它不会将背景设置为红色。您可以尝试将myElement假设为TextBlock!为什么会这样?

3 个答案:

答案 0 :(得分:7)

标签在WPF中没有特殊功能。

这对我有用:

<TextBlock Tag="{Binding Data}"
           x:Name="tb">
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="TextBlock.Tag"
                         Value="XYZ">
                     <Setter Property="TextBlock.Background"
                             Value="Lime" />
                </Trigger>
            </Style.Triggers>        
        </Style>
    </TextBlock.Style>
</TextBlock>

在事件中将Data对象属性设置为“XYZ”。

答案 1 :(得分:7)

Tag是一个从Winforms时代开始的构造(也可能是之前的那个!)。它被用作将对象与UI元素相关联的便利位置,例如带有Button的FileInfo,因此在Button的事件处理程序中,您可以简单地获取事件发送者,将其转换为Button,然后转换{{1对FileInfo的值,你拥有你想要打开的文件所需的一切。

有一种情况,但是,我发现Tag在WPF中很有用。我已经将它用作可以被ContextMenu MenuItem访问的保留点,它不能使用您用来遍历可视树的普通RelativeSource绑定。

Tag

<ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Tag" Value="{Binding ElementName=TheUserControlRootElement}" /> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="_Remove" ToolTip="Remove this from this list" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" Command="{Binding PlacementTarget.Tag.Remove, RelativeSource={RelativeSource AncestorType=ContextMenu}}" /> </ContextMenu> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> ,我无法访问删除命令,该命令在定义此代码段的ContextMenu类中定义。但是我可以将根绑定到UserControl的{​​{1}},我可以通过Tag属性访问。在ListBoxItem内绑定时可以使用相同的技巧,因为同样的限制适用。

答案 2 :(得分:2)

MainWindow.xaml:

<Window x:Class="wpftest.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">
    <Grid>
        <TextBlock x:Name="test" MouseDown="test_MouseDown"
                   Tag="{Binding TestProperty}">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <Trigger Property="Tag" Value="XYZ">
                            <Setter Property="Background" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new TestViewModel();
    }

    private void test_MouseDown(object sender, MouseButtonEventArgs e)
    {
        ((TestViewModel)DataContext).TestProperty = "XYZ";
    }

    private sealed class TestViewModel : INotifyPropertyChanged
    {
        private string _testPropertyValue;

        public string TestProperty
        {
            get { return _testPropertyValue; }
            set
            {
                _testPropertyValue = value;
                var handler = PropertyChanged;
                if(handler != null)
                    handler(this, new PropertyChangedEventArgs("TestProperty"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

更新:Tag属性现在绑定到TestProperty