【问题标题】:ListBox with ComboBox DataTemplate BindingListBox 与 ComboBox DataTemplate 绑定
【发布时间】:2017-07-19 08:36:06
【问题描述】:

我目前在Datatemplate 中有一个Combobox,用于Listbox。我已将Combobox 绑定到string[]。这工作正常。

我想要的是当 Combobox 更改时,Listbox 的索引应该与数组中的字符串相关联。 IE。如果我在Listbox 的第 3 行中选择 Combobox 中的第 4 项,我的数据应该由 Combobox string), int (Listbox index)> 表示,但要保存重复数据我想将此数据用作我的Combobox 绑定。

我在想我可以使用键值对,但我不确定如何将其绑定到 DataTemplate 中的 Combobox(或者如果这是这样做的最佳方式)。

注意

显然这意味着每个Combobox 字符串一次只能与一个Listbox 索引相关联。 因此,我想如果每个Combobox 字符串只能在Listbox 中设置一次,即如果我在Listbox 的索引4 中选择Combobox 索引3,那么Listbox 索引5 已经有@987654342 @ 3 应重置为空白。我可能会在 Combobox changed 事件中通过并重置另一个 Comboboxes 如果它是相同的字符串。

示例

好的,下面的绑定工作;

<Window.Resources>
    <DataTemplate x:Key="lbxHeaderDataTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1.5*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Content="{Binding Item1}"></Label>
            <ComboBox Name="cbxTest" Grid.Column="1" ItemsSource="{Binding 
                 Item2}" DisplayMemberPath="Key"></ComboBox>
        </Grid>
    </DataTemplate>

</Window.Resources>
<StackPanel Width="auto" Height="auto">
    <ListBox Name="lbxFields"
             ItemTemplate="{DynamicResource lbxHeaderDataTemplate}" 
             HorizontalContentAlignment="Stretch">
    </ListBox>
</StackPanel>

C#

private List<KeyValuePair<string, int>> cbxOptions2 = new List<KeyValuePair<string, int>>();
cbxOptions2.Add(new KeyValuePair<string, int>("", 0));
cbxOptions2.Add(new KeyValuePair<string, int>("Identifier", 0));
cbxOptions2.Add(new KeyValuePair<string, int>("Family Identifier", 0));
cbxOptions2.Add(new KeyValuePair<string, int>("File Path", 0));
for (int i = 0; i < 10; i++)
{
    lbxDatFields.Items.Add(new Tuple<string, List<KeyValuePair<string, int>>>((i * 10).ToString(), cbxOptions2));
}

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    这就是我最终使用的。请随时提出更好的答案。

    <Window.Resources>
        <DataTemplate x:Key="lbxHeaderDataTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1.5*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label Content="{Binding Item1}"></Label>
                <ComboBox Name="cbxTest" Grid.Column="1" ItemsSource="{Binding 
                     Item2}" DisplayMemberPath="Key"
                    SelectionChanged="cbxTest_SelectionChanged"></ComboBox>
            </Grid>
        </DataTemplate>
    
    </Window.Resources>
    <StackPanel Width="auto" Height="auto">
        <ListBox Name="lbxFields"
                 ItemTemplate="{DynamicResource lbxHeaderDataTemplate}" 
                 HorizontalContentAlignment="Stretch">
        </ListBox>
    </StackPanel>
    

    C#

    private Dictionary<string, int> cbxOptions2 = new Dictionary<string, int>();
    cbxOptions2.Add("", 0);
    cbxOptions2.Add("Identifier", 0);
    cbxOptions2.Add("Family Identifier", 0);
    cbxOptions2.Add("File Path", 0);
    for (int i = 0; i < 10; i++)
    {
        lbxDatFields.Items.Add(new Tuple<string, Dictionary<string, int>>((i * 10).ToString(), cbxOptions2));
    }
    
    private void cbxTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox test = (ComboBox)sender;
        DependencyObject parent = VisualTreeHelper.GetParent(test);
        Label currentTxt = null;
        foreach (object o in ((Grid)parent).Children)
        {
            if (o is Label)
            {
                currentTxt = (Label)o;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 2010-11-13
      • 2017-06-05
      • 1970-01-01
      • 2013-03-23
      • 2011-07-13
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多