简体   繁体   中英

ListBox with ComboBox DataTemplate Binding

I currently have a Combobox in a Datatemplate for a Listbox . I've bound the Combobox to a string[] . This is working fine.

What I would like is when a Combobox is changed, the index of the Listbox should be associated with the string in the array. Ie if I select the 4th item in the Combobox in the 3rd line of the Listbox my data should be represented by < string ( Combobox string), int ( Listbox index)> but to save on duplicated data I would like to use this data as my Combobox binding.

I was thinking I could use a key value pair but I'm unsure how I would bind this to the Combobox that is in a DataTemplate (or if this is the best way of doing this).

Note

Obviously this means that each Combobox string can only be associated with one Listbox index at a time. Therefore, I would like if each Combobox string could only be set once in the Listbox , ie if I select Combobox index 3 in index 4 of the Listbox then Listbox index 5 which already had Combobox 3 should be reset to blank. I will probably in the Combobox changed event go through and reset the other Comboboxes if it is for the same string.

Sample

OK so the following binding works;

<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));
}

This was what I ended up using. Please feel free to suggest a better answer.

<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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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