[英]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.