WPF:区分ItemsCOntrol中的项目

时间:2018-04-18 09:23:37

标签: c# wpf data-binding itemscontrol

烨,

我使用相同的ItemsControl来显示多个组合框。 防爆。根据绑定到ItemsControl的项目列表,使用相同的模板显示某些名称的组合框,有时使用带有一些名字的组合框。  在模板中,ComboBox的SelectedValue只绑定了一个名为“ReceivedCBValue”的属性。 所以,有时候,选中的值会给我一些名字,有时候是名字。 但我怎么知道我是否有名字或名字?

另一方面,我对文本框有同样的问题: 相同的模板用于显示某些时间文本框,它将接收名称,有时文本框将接收名字。同样,模板中的文本框仅绑定到一个属性“receivedTextValue”。 我同时显示了几个文本框,当我在其中一个文本框中键入一些文本时,其他文本框显示我刚输入的文本。最后,我有多个具有相同值的文本框。绝对无用。

  <ItemsControl Background="BurlyWood" Margin="20" Name="IcChoice"
                  ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="350"
                  Height="200" 
                  Tag="{Binding Tag, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"                      
                  >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="5" Background="Brown"
                      Visibility="{Binding Visible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>


                    <!-- ComboBox Source is bound to the Children List, whereas its selectedValue is bound to ReceivedCBValue on the main DataContext ( Hello), so to the ViewModel -->
                    <ComboBox Grid.Column="0"
                              ItemsSource="{Binding Children}"
                              Visibility="{Binding ComboBoxVisibility}"
                              SelectedValue="{Binding DataContext.ReceivedCBValue, ElementName=Hello, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                              Grid.Row="1" />
                    <ComboBox Grid.Column="0"
                              ItemsSource=""></ComboBox>

                    <TextBox
                        Text="{Binding DataContext.ReceivedText,
                        ElementName=Hello,
                        UpdateSourceTrigger=PropertyChanged,
                        Mode=TwoWay}"                           
                        Grid.Row="1" />

1 个答案:

答案 0 :(得分:0)

我想我明白我错了,为什么。 我使用那些ItemsControls及其内容在List(或ObservableCollection)上应用过滤器,在我理解之前,我想要使用值类型的“直接”应用过滤器(这就是为什么我想在我的视图模型中获取值) typedValue - &gt; ViewModel.ReceivedValue - &gt; applyFilter(ViewModel.ReceivedValue);)。 现在,我知道我必须在DisplayedIteml对象的属性中设置所有值,将此对象发送到我的viewModel(或类似的东西),并将该过滤器应用于该对象的值。