【问题标题】:WPF Binding to dictionary with classesWPF 使用类绑定到字典
【发布时间】:2015-10-28 06:40:01
【问题描述】:

我是 WPF 编程新手,不是 C# 编码专业人士。

所以我有以下问题: 我有很多不同的组合框和文本框。 我想在后台将它们绑定到一个变量,因为我想用数据库中的数据填充组合框。

所以我在文件
liste.xaml.cs中创建了以下内容:

private Dictionary<string, Filter> _ctrlFilter;

public Dictionary<string, Filter> ctrlFilter {
    get { return _ctrlFilter; }
    set { _ctrlFilter = value; }
}

当我加载 liste.xaml 时,它会使用以下代码初始化 ctrlFilter 字典:

ctrlFilter = new Dictionary<string, Filter>
{
  //ComboBox
  {"hersteller", new Filter("Hersteller", typeof(ComboBox)) },
  {"fahrzeugart", new Filter("Fahrzeugart", typeof(ComboBox), false) },

  //TextBox
  {"baujahr", new Filter("Baujahr", typeof(TextBox), true, typeof(short)) },
  {"anschaffungswert", new Filter("Anschaffungswert", typeof(TextBox), true, typeof(decimal)) },
  {"finanz_restwert", new Filter("Restwert", typeof(TextBox), true, typeof(decimal)) },

  //Others
  {"zugelassen", new Filter("Zugelassen", typeof(DatePicker), true, typeof(DateTime)) },
  {"vstabzug", new Filter("Vorsteuerabzug", typeof(CheckBox), true, typeof(bool)) },
};

所以过滤器类(也在 liste.xaml.cs 中)看起来像这样:

public class Filter
{
    public string FilterName;
    public Type ControlType;
    public Type FromToType;
    public bool Load;
    public ObservableCollection<object> Items;
    public object SelectedFilter;
    public object From;
    public object To;

    public Filter( string name, Type type, bool load = true, Type ftType = null)
    {
        Reset();
        FilterName = name;
        ControlType = type;
        Load = load;
        FromToType = ftType;
    }

    public void Reset()
    {
        From = null;
        To = null;
        SelectedFilter = null;
        Items = new ObservableCollection<object>();
    }
}

现在我将其他 xaml 文件动态加载到 liste.xaml 文件中。例如 Fahrzeug_Allgemein.xaml

我有组合框,看起来像这样:

            <StackPanel>
                <Label Content="Hersteller:" Target="{Binding ElementName=cmb_hersteller, Mode=OneWay}"/>
                <ComboBox x:Name="cmb_hersteller" Fahrzeuge:FilterExtension.FilterName="hersteller"  Width="120" ItemsSource="{Binding ctrlFilter[hersteller].Items}" SelectedIndex="0" SelectionChanged="cmb_hersteller_SelectionChanged"/>
            </StackPanel>

您看到我试图从 Filter 类中获取 Items 属性,但它不起作用。在 Visual Studio 2015 输出中它说:

System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''Filter' (HashCode=44888241)'. BindingExpression:Path=ctrlFilter[hersteller].Items; DataItem='liste' (Name=''); target element is 'ComboBox' (Name='cmb_hersteller'); target property is 'ItemsSource' (type 'IEnumerable')

我已经阅读了一些关于 INotifyPropertyChanged 的内容,但我不知道如何在我的情况下使用它。

如果你能帮助我,那就太好了。 谢谢。

【问题讨论】:

标签: c# wpf xaml dictionary data-binding


【解决方案1】:

没想到答案这么简单。我只需要将 { get; set; } 添加到 Items 属性。

所以代码现在看起来像这样:

public class Filter
{
    public string FilterName;
    public Type ControlType;
    public Type FromToType;
    public bool Load;
    public ObservableCollection<ComboBoxItem> Items { get; set; }
    ...

我认为这意味着 Items 属性只能通过此添加进行绑定。

【讨论】:

  • 准确地说,在你添加get/set之前,它只是一个字段,而不是一个属性。
【解决方案2】:

我的声誉太低,无法对此发表评论,但如果您还没有评论,您应该 read up on properties in c#data binding。我阅读了这些链接以及它们包含的额外链接,这大大增加了我的理解。

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 2016-11-20
    • 2012-09-26
    • 2016-03-13
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多