我有一个带有datatemplate的列表框,其中包含许多绑定到我的集合的控件。

我想要的是根据在同一行中的一个其他组合框中选择的值来更改这些组合框中的一个的项目源。我不想更改列表框中其余行中所有相应组合框的itemsource。

如何仅处理所选行中的控件。

这是否更容易尝试使用WPF数据网格?

感谢。

有帮助吗?

解决方案

使用ListBox实际上更容易,因为DataTemplate定义了行的所有控件。

我认为最简单的方法是在绑定上使用转换器。您将第二个ComboBox的ItemsSource绑定到第一个ComboBox的SelectedItem:

<myNamespace:MyConverter x:Key="sourceConverter" />

<StackPanel Orientation="Horizontal>
    <ComboBox x:Name="cbo1" ... />
    ...
    <ComboBox ItemsSource="{Binding SelectedItem, ElementName=cbo1, Converter={StaticResource sourceConverter}}" ... />
    ...
</StackPanel>

请注意,如果您需要来自Row的DataContext的其他信息,可以将其设置为MultiBinding和IMultiValueConverter,并通过执行以下操作轻松传入DataContext:

<MultiBinding Converter="{StaticResource sourceConverter}">
    <Binding />
    <Binding Path="SelectedItem", ElementName="cbo1" />
</MultiBinding>

然后,在您的转换器类中,为了获得正确的项目来源,您可以做任何事情。

其他提示

获取该特定组合框的SelectionChanged事件,并在事件中设置其他组合框的Itemsource。

private void cmb1SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  cmboBox2.ItemSource = yourItemSource;
}

最好还是获取listview的SelectionChaged事件并处理它。

 private void OnlistviewSelectionChanged( object sender, SelectionChangedEventArgs e )
 {
    // Handles the selection changed event so that it will not reflect to other user controls.
    e.Handled = true;
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top