【问题标题】:Databinding a collection to labels and dropdown将集合数据绑定到标签和下拉列表
【发布时间】:2014-04-26 00:31:07
【问题描述】:

我正在尝试创建一个System.Windows.Window 以允许用户将用户输入的食物映射到预先选择的类别数量。例如,用户输入“apple”,然后从下拉列表中选择 Fruit,然后输入“banana”并选择 Fruit,然后输入“carrot”并选择蔬菜等(一对多映射)。为此,我相信我需要一个文本框和一个下拉菜单,其中包含一个 ListView 或 GridView。

我想做 DataBinding 以使这更容易,但由于我是 WPF 新手,即使是简单的东西也很难(更不用说像我描述的那样创建复杂的情况了)。为了让事情变得更复杂,Window 需要能够加载用户之前的选择。我假设我需要将两件事传递给 Window 构造函数,以使其成为公共属性(用于访问 DataBinding):

  1. 填写类别下拉列表的所有可能类别的列表。例如,

    this.Category = new List<string>{ "Fruit", "Vegetable", "Grain" };
    
  2. 先前选择的字典(将“食物”字符串映射到其中一个类别)。在这个例子中,

    this.PriorSelections = new Dictionary< string, string >() { {"banana", "fruit"} };
    

如何在 .NET 3.0 中使用数据绑定在 WPF 中实现这一点? GridViewColumn 声明是我现在最受困扰的地方,但非常欢迎所有其他见解。如果 GridView 或 ListView 是不正确的容器,请告诉我另一个容器是否最好。谢谢!

【问题讨论】:

    标签: c# wpf gridview data-binding drop-down-menu


    【解决方案1】:

    使用可由用户填写的 DataGrid 的小示例:

    <Window x:Class="TestWpfProject.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TestWpfProject="clr-namespace:TestWpfProject"
            Title="MainWindow" Height="213" Width="404"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            >
      <Grid>
        <DataGrid CanUserAddRows="True" CanUserDeleteRows="True" ItemsSource="{Binding Fruits}" AutoGenerateColumns="False">
          <DataGrid.Resources>
            <TestWpfProject:Categories x:Key="comboItems"/>
          </DataGrid.Resources>
          <DataGrid.Columns>
            <DataGridTextColumn MinWidth="100" Header="Name" Binding="{Binding Name}"/>
            <DataGridComboBoxColumn MinWidth="100" Header="Type" SelectedItemBinding="{Binding Category}" ItemsSource="{Binding CategoryList, Source={StaticResource comboItems}}"/>
          </DataGrid.Columns>
        </DataGrid>    
      </Grid>
    </Window>
    

    请注意 Datacontext 设置为 self(主窗口)=> DataContext="{Binding RelativeSource={RelativeSource Self}}"

    在您的代码中,您必须提供这样的可绑定对象:

    namespace TestWpfProject
    {
      public partial class MainWindow : Window
      {
        public class Fruit
        {
          public string Name { get; set; }
          public string Category { get; set; }
        }
    
        public MainWindow()
        {
          Fruits = new ObservableCollection<Fruit>();
          InitializeComponent();
        }
    
        public ObservableCollection<Fruit> Fruits
        {
          get; set; 
        }
      }
    
      public class Categories
      {
        public List<string> CategoryList { get; set; }
        public Categories()
        {
          CategoryList = new List<string>() { "fruit", "vegetable", "grain" };
        }
      }
    }
    

    数据将存储在 Fruits 可观察列表对象中。

    【讨论】:

    • 这看起来是一个近乎完美的答案(我可能仍然这样标记它,因为我一开始也没有提到这个标准),但我必须在 .NET 3.0 中这样做 -非常抱歉!我真的很感谢你为我嘲笑这个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2010-11-25
    相关资源
    最近更新 更多