开发者

WPF TwoWay Binding some elements to ObservableCollection

开发者 https://www.devze.com 2023-04-11 09:21 出处:网络
I need bind some ComboBoxes to one ObservableCollection. I have this ListView. <ListView x:Name=\"lwCoefTables\" Grid.Column=\"1\" ItemsSource=\"{Binding Source={StaticResour开发者_如何学Cce Colle

I need bind some ComboBoxes to one ObservableCollection. I have this ListView.

<ListView x:Name="lwCoefTables" Grid.Column="1" ItemsSource="{Binding Source={StaticResour开发者_如何学Cce CollectionCoefContainers}}">
<ListView.ItemTemplate>
    <DataTemplate>
        <ComboBox x:Name="cmbCoefTableTypes" ItemsSource="{Binding Source={StaticResource CollectionCoefLinksTable}}"  
                SelectedItem="{Binding CoefLinksTableType, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" 
                HorizontalAlignment="Left" Width="180" DisplayMemberPath="Name">
        </ComboBox>
    </DataTemplate>
</ListView.ItemTemplate>

I want bind my collection to all ComboBoxes and save selected items for each ComboBox. If I fill one collection and bind it to all comboboxes in TwoWay mode I get this:

Picture

I think I need helper class that will contain some similar collections. How to do that?


So I assume the CoefLinksTableType property is on the items inside CollectionCoefContainers?

In which case this should work, unless you have the same instance repeated inside CollectionCoefContainers.

e.g.

Something like this would behave as you describe.

var vm = new VM();
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);

The solution would be

CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());

It might be useful to have you definitions of CollectionCoefContainers and CollectionCoefLinksTable

0

精彩评论

暂无评论...
验证码 换一张
取 消