views:

29

answers:

1

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="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.

A: 

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!

Padel
So I can't use a collection within a collection? I have to make a new object that makes a list of the values I want to use?
Terry T
What i meant is that that`s how the data in the table looks as an object (list of rows, each row having a list of column/column value pair). I didn't use a table to bind to in my example.I suppose you can bind to a table without problems in the same manner as above.Just try to follow how each control is bound to the actual data.I'll try to get an example with a table too but I didn`t have the time.Regards...
Padel
I've tried it and the problem is getting the column name. I can't think of an easy solution to this. The most i could do is bind the values of each row to the combo box (without the column name) and have a combo box for each row.For this bind the list view setting the path to the table property({Binding Path=Table}), the combo box to the ItemsArray ({Binding Path=ItemsArray}) and have the label bind to the current object ({Binding}).Hope it helps somehow...
Padel