wpf将文本框绑定到List - MainWindow.List.Item [0]

时间:2015-09-08 06:56:12

标签: c# wpf user-interface textbox updatesourcetrigger

我的MainWindow中有一个静态列表。如果发生更改,则立即设置CurrValue。

public static List<varVisu> varVisuListDll = new List<varVisu>();

在我的班级中,有一个INotifyPropertyChanged实现

    public string m_CurrValue;

    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string CurrValue
    {
        get { return m_CurrValue; }
        set 
        {
            if (value != m_CurrValue)
            {
                //set value
                m_CurrValue = value;
                //notify anyone who cares about it
                Notify("CurrValue");
            }
        }
    }

这很好用,但现在,我想要将Window#2中的Textbox(Text)绑定到此List中的第一个项目(varVisuListDll [0] .CurrValue)

如何将TextBox.Text绑定到此值(Text = {Path,UpdateSourceTrigger ...} ??

<TextBox x:Name="txtManualMode" Text="{Binding ElementName=????, Path=CurrValue, UpdateSourceTrigger=PropertyChanged}" 

我已经测试过(dtgrVariables.ItemSource = MainWindow.varVisuListDll)。这项工作。

请帮帮我..

2 个答案:

答案 0 :(得分:0)

varVisuListDll必须是属性,而不是字段:

private static List<varVisu> varVisuListDll = new List<varVisu>();

public static List<varVisu> VarVisuListDll
{
    get { return varVisuListDll; }
}

然后绑定应该如下所示:

<TextBox Text="{Binding Path=(local:MainWindow.VarVisuListDll)[0].CurrValue}"/>

或者,如果您使用的是比.NET 4更旧的框架:

<TextBox Text="{Binding Path=[0].CurrValue,
                        Source={x:Static local:MainWindow.VarVisuListDll}}"/>

答案 1 :(得分:0)

我已经解决了这个问题。

我在代码中设置了绑定。这项工作很好。

        varVisu v1 = MainWindow.varVisuListDll[1];
        txtManualMode.DataContext = v1;
        Binding binding = new Binding() { Path = new PropertyPath("CurrValue") };
        txtManualMode.SetBinding(TextBox.TextProperty, binding);