WPF 列表框 - 更改数据模板中控件的项目源

发布于 2024-08-04 15:54:41 字数 184 浏览 3 评论 0原文

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

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

如何仅获取所选行中的控件的句柄。

使用 WPF 数据网格是否可以更轻松地尝试执行此操作?

谢谢。

I have a listbox with a datatemplate that holds a number of controls bound to my collection.

What i want is to change the itemsource for one of these comboboxes dependant upon the value selected in one of the other comboboxes in the same row. I don't want to change the itemsource for all corresponding comboboxes within the rest of the rows in the listbox.

How do I get a handle on the control in the selected row only.

Is this something that is easier to try doing witht the WPF datagrid?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-08-11 15:54:41

使用 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>

操作轻松传入 DataContext:

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

请注意,如果您需要来自 Row 的 DataContext 的附加信息,您可以将其设为 MultiBinding 和 IMultiValueConverter,并通过执行以下 在您的转换器类中,执行您必须执行的操作以获得正确的项目源。

This is actually easier with the ListBox, as the DataTemplate defines all the controls for a row.

I think the easiest way is to use a converter on a binding. You will bind your second ComboBox's ItemsSource to the SelectedItem of the first ComboBox:

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

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

Note that if you need additional information from the DataContext of the Row, you can make it a MultiBinding and an IMultiValueConverter, and pass in the DataContext easily by doing:

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

Then, in your converter class, do whatever it is you have to do in order to get the correct items source.

眼前雾蒙蒙 2024-08-11 15:54:41

获取该特定组合框的 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;
 }

Get the SelectionChanged event of that paticular combobox and set the Itemsource of the other combobox inside the event.

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

Also it is better to get the SelectionChaged event of listview and handle it.

 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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文