开发者

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. I have a DataTable object that I set as the itemssource of a combobox. For each row in the DataTable I want a 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. Nothing I try seems to work but heres my shot at the datatemplate in XAML.

<Grid x:Name="LayoutRoot" Background="White" Height="107" Width="358">
    <ComboBox Name="pCombo" ItemsSource="myTa开发者_运维百科ble">
        <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:

<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 (i.e. in the .ctor of the window) like this:

DataContext = this;

I just hope you get the idea behind this!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜