如何将 ListView 的一个 ObservableCollection 的属性绑定到另一个 ListView 的 SelectedItem 的属性?

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

所以我有几个 ListView。第一个绑定到 ObservaleCollection<ComPort>ComPort 的所有属性都可能采用一些预定义的值。其他 ListView 负责该属性:它们显示所有可能的(预定义)值,并且 SelectedItem 应该是第一个 ObservaleCollection.[=19 中 ComPort 的 属性 的当前值=]

我无法附上图片所以这里是一张外部图片,它会使情况变得干净: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>

我试图创建一个 class ComPort 的实例来保存第一个 ListView 中所选项目的当前值,但无论如何我无法在没有帮助的情况下处理它。这个任务应该怎么解决?

1) 不要在 PortsList ListView 上处理 SelectionChanged,而是像这样将您的复选框绑定到 ListViewItemsPanel:

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

2) 将 x:Name 添加到您的第一个列表框,例如 x:Name="ComPortLB"; 3)移除SystemPortsList上的DataContext; 4) 像这样修复 SystemPortsList 上的 SelectedItem:

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

我没有测试过这段代码的任何部分,也有一段时间没有做这种事情了,所以对于错误我深表歉意,但这应该会让你更接近。由于您没有提供足够的信息,我还不得不对您的 类 做出一些假设。