eexaxa
eexaxa

Reputation: 37

How to bind properties of one ObservableCollection of ListView to properties of SelectedItem of another ListView?

So I have a few ListViews. The first is binded to ObservaleCollection<ComPort>. All properties of ComPort may take some predefined values. Other ListViews are responsible for that properties: they show all that possible (predefined) values and SelectedItem should be the current value of that property of ComPort from the first ObservaleCollection.

I can't attach images so here is an external picture, it would make the situation clean: https://i.sstatic.net/ZBRRx.png

<Window.Resources>
    <ResourceDictionary x:Name="rd">
        <l:ComPorts x:Key="vComPorts"/>
        <l:SystemPorts x:Key="vSystemPorts"/>
        <l:BaudRates x:Key="vBaudRate"/>
        <l:Parities x:Key="vParities"/>
        <l:DataBits x:Key="vDataBits"/>
        <l:StopBits x:Key="vStopBits"/>
        <l:Timeouts x:Key="vTimeouts"/>
        <l:ComPort x:Key="vSelectedPort"/>
    </ResourceDictionary>
</Window.Resources>

...

<ListView
            Name="PortsList"
            Grid.Row="1"
            Grid.Column="0"
            Margin="5"
            VerticalAlignment="Stretch"
            ItemsSource="{StaticResource vComPorts}"
            DataContext="{StaticResource vComPorts}"
            SelectedValuePath="PortName"
            SelectedValue="{Binding ElementName=SystemPortsList, Path=SelectedItem.Value}"
            SelectionChanged="PortsList_SelectionChanged"
            MouseDoubleClick="PortsList_MouseDoubleClick">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox />
                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <ListView 
            x:Name="SystemPortsList"
            Margin="5"
            VerticalAlignment="Stretch"
            DataContext="{Binding Source={StaticResource vSelectedPort}}"
            ItemsSource="{Binding Source={StaticResource vSystemPortsView}}"
            SelectedItem="{Binding Source={StaticResource vSelectedPort}, Path=PortName}"
            MouseEnter="SystemPortsList_Refresh"
            MouseLeave="SystemPortsList_Refresh"
            Grid.Row="1"
            Grid.Column="1" SelectionChanged="SystemPortsList_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Name="tb" Margin="5,0,0,0" Text="{Binding Path=Name}" />
                    </StackPanel>
            </ListView.ItemTemplate>
        </ListView>

I've tried to make an instance of class ComPort for saving current value of selected item from the first ListView, but anyway I can't cope with it without help. How this task should be solved?

Upvotes: 0

Views: 160

Answers (1)

Chris Bordeman
Chris Bordeman

Reputation: 226

1) Instead of handling SelectionChanged on the PortsList ListView, bind your checkbox to the ListViewItemsPanel like so:

<CheckBox IsChecked={Binding IsSelected, RelativeSource=Parent/>

2) Add an x:Name to your first ListBox, say x:Name="ComPortLB"; 3) Remove DataContext on SystemPortsList; 4) Fix SelectedItem on SystemPortsList like so:

SelectedValue="{Binding ElementName=ComPortLB, Path=SelectedValue.PortName}"

I haven't tested any of this code and I haven't done this kind of stuff for a while, so I apologize for errors, but it should get you closer. I've also had to make some assumptions about your classes since you don't provide enough information.

Upvotes: 0

Related Questions