简体   繁体   中英

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

So I have a few ListView s. The first is binded to ObservaleCollection<ComPort> . All properties of ComPort may take some predefined values. Other ListView s 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: http://i.stack.imgur.com/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?

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.

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