wpf - 将 WPF ComboBox 绑定(bind)到 ListBox DataTemplate 中的不同 ItemsSource

标签 wpf combobox listbox datatemplate data-binding

我有一个列表框,其数据模板中包含一个文本框和一个组合框:

<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBox Text="{Binding Path=Name}" />
                    <ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我想将列表框绑定(bind)到对象集合(我已经成功完成),但我希望上述数据模板中的组合框将其 itemssource 设置为窗口上的本地属性(整数数组)。我仍然希望组合框在其所选项目和对象集合上的属性之间具有双向绑定(bind)...

我的代码如下: PitcherListBox.DataContext = this;

基本上最后,我希望列表框中的组合框具有与列表框本身不同的项目源。我似乎不知道如何在 XAML 中更改 ComboBox 的 ItemsSource。有人可以给我一些反馈吗?谢谢!

最佳答案

试试这个:

<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />

请注意,您需要将 YourWindowTypeHere 替换为包含 LocalArrayOfIntsProperty 的窗口类型!另请记住,您需要为该类型定义一个 xml 命名空间!

关于wpf - 将 WPF ComboBox 绑定(bind)到 ListBox DataTemplate 中的不同 ItemsSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2695954/

相关文章:

C# - 用 DataTable 填充组合框

WPF:按钮未占据列表框中的完整空间

listbox - WP7 难以将数据绑定(bind)到列表框项目源 - 不会刷新

c# - 在 ObservableCollection 内绑定(bind) ObservableCollection

wpf - System.BadImageFormatException 与 Sqlite

wpf - MVVM组合框 ListView 中的选定项绑定(bind)

c# - 如何在 C# 中从 ComboBox 访问 emp_id 和名称

c# - ListBox,实现相同接口(interface)的不同对象的列表

c# - 从另一个线程更新 ObservableCollection

c# - 如何在不根据当前位置 X 和 Y 更改纵横比的情况下调整旋转控件的大小?