简体   繁体   English

选择组合框项目时,DataTrigger不起作用

[英]DataTrigger does not work when selecting combobox item

I have a combo box that is to be filled dynamically. 我有一个组合框,可以动态填充。

When the user selects an item from the combo box, a label needs to show up. 当用户从组合框中选择一个项目时,需要显示一个标签。

This works when I use a static combo box, but when the combo box is dynamic it does not. 当我使用静态组合框时此方法有效,但是当组合框为动态组合框时则无效。 I'm pretty sure it has to do with the Name field of the combo box item. 我很确定这与组合框项目的“ Name字段有关。

Here is the code : 这是代码:

C#: C#:

public ObservableCollection<ComboBoxItem> cbItems { get; set; }
public ComboBoxItem SelectedcbItem { get; set; }

public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        cbItems = new ObservableCollection<ComboBoxItem>();

        var cbItem = new ComboBoxItem { Content = "<--Select-->"};
        SelectedcbItem = cbItem;
        cbItems.Add(cbItem);

        var cbItem1 = new ComboBoxItem();
        cbItem1.Content = "Option 1";
        cbItem1.Name = "iOne";

        var cbItem2 = new ComboBoxItem();
        cbItem2.Content = "Option 2";
        cbItem2.Name = "iTwo";

        cbItems.Add(cbItem1);
        cbItems.Add(cbItem2);


    }

XAML: XAML:

<ComboBox Width="130" ItemsSource="{Binding cbItems}" SelectedItem="{Binding SelectedcbItem}" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalContentAlignment="Center"/>

<Label Content="One is shown" Grid.Column="0" Grid.Row="6">
    <Label.Style>
        <Style TargetType="Label">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=iOne, Path=IsSelected}"    Value="True">
                    <Setter Property="Visibility"  Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
   </Label>

Use this XAML 使用此XAML

<ComboBox x:Name="cb"
          ItemsSource="{Binding CbItems}" SelectedItem="{Binding SelectedCbItem}" .../>

<Label Content="One is shown" ...>
    <Label.Style>
        <Style TargetType="Label">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=cb, Path=SelectedItem}"
                             Value="Option 1">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>

with this code behind: 下面的代码:

public List<string> CbItems { get; }
public string SelectedCbItem { get; set; }

public MainWindow()
{
    InitializeComponent();
    cbItems = new List<string> { "Option 1", "Option 2" };
    DataContext = this;
}

Alternatively: 或者:

<DataTrigger Binding="{Binding ElementName=cb, Path=SelectedIndex}" Value="0">

If you want to make the DataTrigger use a Binding to the source property SelectedCbItem , like 如果要使DataTrigger使用对源属性SelectedCbItem的绑定,例如

<DataTrigger Binding="{Binding SelectedCbItem}" Value="Option 1">

that property must fire a property change notification, eg the PropertyChanged event of the INotifyPropertyChanged interface. 该属性必须触发属性更改通知,例如INotifyPropertyChanged接口的PropertyChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM