WPF绑定文本框到字典条目 [英] WPF binding textbox to dictionary entry

查看:676
本文介绍了WPF绑定文本框到字典条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将wpf文本框绑定到viewmodel中放置的字典。视图模型用作视图的数据标签。
我发现很多例子,听起来很简单,但对我来说不行。



查看:


TextBox x:Name =txbTestGrid.Row =10Grid.Column =2Text ={Binding MyDict [First]}


ViewModel:

  public Dictionary< string,string> ; MyDict = new Dictionary< string,string> 
{
{First,Test1},
{Second,Test2}
};

我尝试我找到的所有变体

  Text ={Binding MyDict [First]}
Text ={Binding Path = MyDict [First]}
Text ={Binding MyDict [First] .Text}
Text ={Binding MyDict [First] .Value}

但无效,文本框为空。任何想法?

解决方案

您的代码中有一个Binding错误,因为 MyDict 不是财产。您必须绑定到属性而不是绑定到字段



$ code> System.Windows.Data错误:40:BindingExpression路径错误:'MyDict'属性在'object''''MainWindow'(Name ='')'上找不到。 BindingExpression:路径= MyDict [第一]; DataItem ='MainWindow'(Name ='');目标元素为'TextBox'(Name ='textBox1');目标属性为'Text'(类型'String')

更改MyDict 字段属性如下所示

 私人字典< string,string> _MyDict; 

public Dictionary< string,string> MyDict
{
get {return _MyDict; }
set {_MyDict = value; }
}

在您的 ViewModel initialize MyDict。

  MyDict = new Dictionary< string,string> 
{
{First,Test1},
{Second,Test2}
};

以下两个变体将不起作用MyDict [key]返回一个 string string 没有文本属性。其他两个变体应该工作。

  Text ={Binding MyDict [First] .Text}
Text ={Binding MyDict [First] .Value}


i try to bind a wpf textbox to a dictionary placed in a viewmodel. The viewmodel is used as datacontext for the view. I found a lot of examples and it sounds simple, but it will not work for me.

View:

TextBox x:Name="txbTest" Grid.Row="10" Grid.Column="2" Text="{Binding MyDict[First]}"

ViewModel:

public Dictionary<string, string> MyDict = new Dictionary<string, string>
        {
            {"First", "Test1"},
            {"Second", "Test2"}
        };

I try all variants i found

Text="{Binding MyDict[First]}"
Text="{Binding Path=MyDict[First]}"
Text="{Binding MyDict[First].Text}"
Text="{Binding MyDict[First].Value}"

But nothing works, textbox is empty. Any idea?

解决方案

There is a Binding error in your code because MyDict is not a property. You have to bind to a Property and not to a Field

System.Windows.Data Error: 40 : BindingExpression path error: 'MyDict' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=MyDict[First]; DataItem='MainWindow' (Name=''); target element is 'TextBox' (Name='textBox1'); target property is 'Text' (type 'String')

Change the MyDict Field to a Property like shown below

    private Dictionary<string, string> _MyDict;

    public Dictionary<string, string> MyDict
    {
        get { return _MyDict; }
        set { _MyDict = value; }
    }

In the constructor of your ViewModel initialize MyDict.

        MyDict = new Dictionary<string, string>
        {
            {"First", "Test1"},
            {"Second", "Test2"}
        };

The following two variants will not work as MyDict["key"] returns a string and string does not have a Text or Value property. The other two variants should work.

Text="{Binding MyDict[First].Text}"
Text="{Binding MyDict[First].Value}"

这篇关于WPF绑定文本框到字典条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆