如何将字符串的静态资源用于与SelectedItem组合框绑定的DataTrigger的值



我正在使用一些组合蛋白,即它们的组合蛋白是一组字符串的静电。我想通过使用其seleteditem而不是SelectedIndex选择另一个组合蛋白的项目之一,以更改Combox的可见性。

为此,我编写了以下代码,但是Visual Studio显示了此错误消息:"使用'DataTrigger'之后(密封(无法修改。"

<Window x:Class="CB.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:CB"
    mc:Ignorable="d"
    Title="MainWindow" Height="177" Width="179">
<Window.Resources>
    <system:String x:Key="Str1">String1</system:String>
    <system:String x:Key="Str2">String2</system:String>
    <system:String x:Key="Str3">String3</system:String>
</Window.Resources>
<StackPanel>
    <ComboBox x:Name="CB1">
        <ComboBoxItem Content="{StaticResource Str1}"/>
        <ComboBoxItem Content="{StaticResource Str2}"/>
        <ComboBoxItem Content="{StaticResource Str3}"/>
    </ComboBox>
    <ComboBox x:Name="CB2">
        <ComboBox.Style>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=CB1, Path=SelectedItem}" Value="{StaticResource Str3}">
                                                                              <!--Error ^: After a 'DataTrigger' is in use (sealed), it cannot be modified. --> 
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
        <ComboBoxItem Content="{StaticResource Str1}"/>
        <ComboBoxItem Content="{StaticResource Str2}"/>
        <ComboBoxItem Content="{StaticResource Str3}"/>
    </ComboBox>
</StackPanel>

您是否可以使用WPF代码来帮助我解决这个问题?

编辑:请有人回答我的问题!

您可以使用MultidatatRigger而代替DataTrigger并使用ComboboxItemToStRingConverter:

public class ComboBoxItemToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var combo = value as ComboBoxItem;
        string content = combo?.Content.ToString();
        return content;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后您使用:

<Window.Resources>
    ...
    <local:ComboBoxItemToStringConverter x:Key="ObjectToStringConverter" />
</Window.Resources>
<ComboBox.Style>
    <Style TargetType="ComboBox">
        <Setter Property="Visibility" Value="Visible" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=CB1, Path=SelectedItem, Converter={StaticResource ComboBoxItemToStringConverter}}" Value="{StaticResource Str3}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility" Value="Collapsed" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox.Style>

我希望它对您有用。

编辑:如果您不想使用C#代码。而不是使用" SelectedItem"使用文本:

<Condition Binding="{Binding ElementName=CB1, Path=Text}" Value="{StaticResource Str3}" />

并删除转换器。

最新更新