WPF组合框和多个复选框

时间:2011-03-22 07:36:49

标签: wpf xaml binding

在XAML中,你如何在左侧的列表或网格中有一个组合框和右侧多个复选框的直线?

假设我有一个类似的数据结构。

sudo:

// for combo
class Option
{
  int key {get;set;}
  string value{get;set;}
}

// for checkboxes
class Selection
{
  int key {get;set;}
  string value{get;set;}
  bool isSelected {get;set;}
}


class Item
{
  Item
  {
    selections = new List<Selection>();
    Options = new List<Option>();
  }
  List<Selection> selections {get;set;}
  List<Option> Options{get;set;}      
}

现在这将是项目来源。

List<Item> x = new List<Item>();

Item i = new Item();
i.Selections.add(blah); 25 selections
i.Options.add(blah);  3 checkboxes
x.add(i) 50 combination's. 

control.itemsource = x;

XAML会是什么样子。我陷入困境,因为我完全没有得到它。

...谢谢

1 个答案:

答案 0 :(得分:3)

<ListBox ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>

            <!-- This is your combobox -->
            <DockPanel HorizontalAlignment="Stretch" LastChildFill="False">
                <ComboBox ItemsSource="{Binding Options}" DockPanel.Dock="Left">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding value}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <!-- This is your line of checkboxes -->
                <ListBox ItemsSource="{Binding Selections}" DockPanel.Dock="Right">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding isSelected}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>