首页 > 解决方案 > WPF中根据DataTable List生成几个Comboboxes

问题描述

我想根据数据表列表创建动态组合框。每次我向列表中添加一个新的数据表时,我都希望 UI 生成一个新的组合框,该组合框显示我从表中指定的列。我用 itemscontrol 和 datatemplate 进行了尝试,但没有像我想要的那样工作。

public List<DataTable> DtList
    {
        get { return dtList; }
        set { dtList = value; }
    }


<ItemsControl ItemsSource="{Binding Path=DtList}" >
        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate >
          <DataTemplate>
            <ComboBox 
                          VerticalContentAlignment="Center"
                          HorizontalContentAlignment="Left"
                          Margin="0,0,4,10"
                          Width="200"
                          BorderBrush="{DynamicResource ListBox.Static.Border}"
                          ItemsSource="{Binding DataSet}"
                          DisplayMemberPath="Element Name"
                          SelectedValuePath="ResourceType Name"
                          SelectedValue="{Binding SelectedRelationPath, Mode=TwoWay}" 
                          />
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>

标签: c#wpfxamldata-bindingdatatemplate

解决方案


我刚刚创建了一个样本,它对我有用

 public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
          //  this.DataContext = this; Uncomment this if code behind is your datacontext
            InitializeComponent();
            DataTable t1 = new DataTable();
            t1.Columns.Add("Name");
            t1.Columns.Add("Age");
            t1.Rows.Add("A", "1");
            t1.Rows.Add("B", "2");
            DataTable t2 = new DataTable();
            t2.Columns.Add("Name");
            t2.Columns.Add("Age");
            t2.Rows.Add("A", "1");
            DtList = new List<DataTable> { t1, t2 };
        }
        private List<DataTable> dtList;
        public List<DataTable> DtList
        {
            get { return dtList; }
            set
            {
                dtList = value;
                OnPropertyChanged("DtList");
            }
        }
        private string selectedName;
        public string SelectedName
        {
           get { return selectedName; }
           set
           {
              selectedName = value;
              OnPropertyChanged("SelectedName");
           }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler == null) return;
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

在您的 Xaml 中,将其添加到您的窗口或 UserControl 标记中

x:Name="windowName"

现在这样做

<StackPanel>
        <ItemsControl ItemsSource="{Binding Path=DtList}" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate >
                <DataTemplate>
                    <ComboBox 
                          VerticalContentAlignment="Center"
                          HorizontalContentAlignment="Left"
                          Margin="0,0,4,10"
                          Width="200"
                          ItemsSource="{Binding}"
                          DisplayMemberPath="Name"
                          SelectedValuePath="Age" 
                          SelectedValue="{Binding Path=SelectedName,ElementName=windowName}"
                          />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>

这对我来说很好。


推荐阅读