WPF 数据触发器未按预期触发

发布于 2024-07-05 08:54:09 字数 758 浏览 6 评论 0原文

我有以下 XAML:

<TextBlock Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Margin="0,0,5,0"/>
<TextBlock Text="items selected">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Value="1">
                    <Setter Property="TextBlock.Text" Value="item selected"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

第一个文本块愉快地随 SelectedItems.Count 变化,显示 0,1,2 等。第二个块上的数据触发器似乎永远不会触发来更改文本。

有什么想法吗?

I have the following XAML:

<TextBlock Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Margin="0,0,5,0"/>
<TextBlock Text="items selected">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Value="1">
                    <Setter Property="TextBlock.Text" Value="item selected"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

The first text block happily changes with SelectedItems.Count, showing 0,1,2, etc. The datatrigger on the second block never seems to fire to change the text.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-07-12 08:54:09

或者,您可以用以下内容替换您的 XAML:

<TextBlock Margin="0,0,5,0" Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count}"/>
<TextBlock>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="items selected"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Value="1">
                    <Setter Property="Text" Value="item selected"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

转换器可以解决许多绑定问题,但拥有大量专用转换器会变得非常混乱。

Alternatively, you could replace your XAML with this:

<TextBlock Margin="0,0,5,0" Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count}"/>
<TextBlock>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="items selected"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=EditListBox, Path=SelectedItems.Count}" Value="1">
                    <Setter Property="Text" Value="item selected"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Converters can solve a lot of binding problems but having a lot of specialized converters gets very messy.

昵称有卵用 2024-07-12 08:54:09

DataTrigger 正在触发,但第二个 TextBlock 的文本字段被硬编码为“选定的项目”,因此无法更改。 要查看它的触发,您可以删除 Text="items selected"。

您的问题很适合使用 ValueConverter 而不是 DataTrigger。 以下是如何创建和使用 ValueConverter 来将 Text 设置为您想要的内容。

创建此 ValueConverter:

public class CountToSelectedTextConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value == 1)
            return "item selected";
        else
            return "items selected";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

将命名空间引用添加到转换器所在的程序集:

xmlns:local="clr-namespace:ValueConverterExample"

将转换器添加到您的资源:

<Window.Resources>
    <local:CountToSelectedTextConverter x:Key="CountToSelectedTextConverter"/>
</Window.Resources>

更改您的第二个文本块:

    <TextBlock Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count, Converter={StaticResource CountToSelectedTextConverter}}"/>

The DataTrigger is firing but the Text field for your second TextBlock is hard-coded as "items selected" so it won't be able to change. To see it firing, you can remove Text="items selected".

Your problem is a good candidate for using a ValueConverter instead of DataTrigger. Here's how to create and use the ValueConverter to get it to set the Text to what you want.

Create this ValueConverter:

public class CountToSelectedTextConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value == 1)
            return "item selected";
        else
            return "items selected";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Add the namespace reference to your the assembly the converter is located:

xmlns:local="clr-namespace:ValueConverterExample"

Add the converter to your resources:

<Window.Resources>
    <local:CountToSelectedTextConverter x:Key="CountToSelectedTextConverter"/>
</Window.Resources>

Change your second textblock to:

    <TextBlock Text="{Binding ElementName=EditListBox, Path=SelectedItems.Count, Converter={StaticResource CountToSelectedTextConverter}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文