C# 如何在xaml中写入ParentListBox.SelectedIndex?
我有一个列表框,其中有组合框作为列表项。每个ListItem中的组合框都是使用ListBox的ItemTemplate创建的 现在,假设我有5个列表项。i、 五个组合框C# 如何在xaml中写入ParentListBox.SelectedIndex?,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我有一个列表框,其中有组合框作为列表项。每个ListItem中的组合框都是使用ListBox的ItemTemplate创建的 现在,假设我有5个列表项。i、 五个组合框 For the 1st ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource. For the 2nd ListItem i.e. 1st ComboBox I would like to have
For the 1st ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 2nd ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 3rd ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 4th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 5th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
所以,我想我必须为不同的组合框使用不同的数据源
For the 1st ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 2nd ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource.
For the 3rd ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 4th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
For the 5th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource.
为此,我有以下开始代码:
<ComboBox....>
<ComboBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="0">
<Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue1}"/>
</DataTrigger>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="Non-0">
<Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ComboBox.Style>
</ComboBox>
现在,我的问题是我应该如何使用父列表框的insead。在上面的代码中选择dex,我应该用什么替换Non-0?对于非零部分,不使用
数据触发器
。。。您只需在样式中将其设置为Setter
,使其成为默认值,然后DataTrigger
仅在值为0
时更改数据源
属性。试试这个:
<ComboBox....>
<ComboBox.Style>
<Style>
<Setter Property="DataSource" Value="{Binding
Path=ListCorrespondingToValue2}"/>
<Style.Triggers>
<DataTrigger Binding="ParentListBox.SelectedIndex" Value="0">
<Setter Property="DataSource" Value="{Binding
Path=ListCorrespondingToValue1}"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ComboBox.Style>
</ComboBox>
我不知道你是否能从这里到达那里。为什么不将第一个框中的选定项绑定到ViewModel中的属性,并在选定项的属性的setter中使用适当的itemsource加载下一个框?这不是一个真正的技巧。。。它只是以样式.Setter
设置了一个属性,但我很高兴它有帮助。