简体   繁体   English

在WPF中如何将集合绑定到父控件的itemssource中

[英]In WPF how to bind a collection within the parent control's itemssource

I'm new to WPF so this may be easier than it seems. 我是WPF的新手,所以这可能比看起来容易。 I have a DataTable object that I set as the itemssource of a combobox. 我有一个DataTable对象,将其设置为组合框的itemssource。 For each row in the DataTable I want a ComboBoxItem. 对于数据表中的每一行,我想要一个ComboBoxItem。 For each ComboBoxItem I want to create a label for every column name and a text box for the corresponding value, in the current row, of that column. 对于每个ComboBoxItem,我想为每个列名称创建一个标签,并为该列的当前行中的对应值创建一个文本框。 Nothing I try seems to work but heres my shot at the datatemplate in XAML. 我尝试过的一切似乎都没有效果,但在XAML中,我对datatemplate的看法不胜枚举。

<Grid x:Name="LayoutRoot" Background="White" Height="107" Width="358">
    <ComboBox Name="pCombo" ItemsSource="myTable">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel DataContext="{Binding pCombo.ItemsSource.Columns}">
                    <TextBlock Text="{Binding ColumnName}"></TextBlock>
                </StackPanel>
                <StackPanel DataContext="{Binding pCombo.ItemsSource.Rows}">
                    <TextBox Text="{Binding RowValue}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

I know all my Bindings are wrong I just can't figure out what should be there instead. 我知道我所有的绑定都是错误的,我只是想不出应该在那里。 Thanks for anyone that helps me out. 感谢任何帮助我的人。

XAML: XAML:

<ListView ItemsSource="{Binding Path=Tbl}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Label Content="{Binding Path=Key}"></Label>
                                <Label Content="{Binding Path=Value}"></Label>
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Code behind: 后面的代码:

private object tbl = new[]
    {
    new[] {
              new KeyValuePair<string, string>("col1", "val1"), new KeyValuePair<string, string>("col2", "val1")
          },
    new[] {
              new KeyValuePair<string, string>("col1", "val2"), new KeyValuePair<string, string>("col2", "val2")
          },
    new[] {
              new KeyValuePair<string, string>("col1", "val3"), new KeyValuePair<string, string>("col2", "val3")
          }
    };

    public object Tbl { get { return tbl; } set { tbl = value; } }

Don't forget to set the DataContext (ie in the .ctor of the window) like this: 不要忘记像这样设置DataContext(即在窗口的.ctor中):

DataContext = this;

I just hope you get the idea behind this! 我只是希望您能理解这个想法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ItemsControl中的DataTemplate中的WPF UserControl - 如何绑定到ItemsSource的父级 - WPF UserControl in DataTemplate within ItemsControl - how to bind to parent of ItemsSource 如何将WPF Datagrid项目源绑定到集合集合? - How to bind WPF Datagrid itemssource to Collection of Collections? 无法在WPF用户控件内绑定DataGrid的ItemsSource - Cannot bind DataGrid's ItemsSource inside WPF User Control 如何将嵌套的ItemsControl ItemsSource绑定到嵌套在父ItemControl的绑定项目内的ObservableCollection - How to bind nested ItemsControl ItemsSource to an ObservableCollection nested within parent ItemControl's bound item 如何绑定WPF ContextMenu ItemsSource? - How to bind WPF ContextMenu ItemsSource? 如何在WPF中绑定到集合中的集合 - How to bind to a collection within a collection in WPF 如何在WPF中使用ObservableCollection绑定ItemsSource - How to Bind ItemsSource with ObservableCollection in WPF 如何在TabControl的ItemsSource绑定到WPF中的列表时设计TabPage? - How to design a TabPage when the TabControl's ItemsSource is Bind to a List in WPF? 如何将集合绑定到WPF中的自定义控件 - how to bind collection to custom control in wpf 如何在WPF中将datagrid与两个itemSource绑定? - How to bind datagrid with two itemsSource in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM