textbox Datacontext是一个列表索引

时间:2013-07-19 21:43:45

标签: wpf datacontext

是否可以将文本框的datacontext设置为List的特定索引>?

我有一个列表,我用于图表的数据上下文,我已经能够使用textchanged上的代码访问数据:

        double d;
        if (!double.TryParse(point4.Text, out d))
        {
            d = 0;
        }
        ValueList.valueList[3] = new KeyValuePair<string, double>("4th", d);

        if (chart1 != null)
        {
            chart1.Refresh();
        }

但如果可能的话,我只想使用数据上下文。

编辑以添加我当前的课程:

public class GraphItems
{
    public string Key { get; set; }
    public double Data { get; set; }

    public GraphItems(string K, double D)
    {
        Key = K;
        Data = D;
    }
}

public class GraphData : ObservableCollection<GraphItems>
{
    public GraphData()
    {
        Add(new GraphItems("1st", 0));
        Add(new GraphItems("2nd", 4));
        Add(new GraphItems("3rd", 3));
        Add(new GraphItems("4th", 2));
    }
}

1 个答案:

答案 0 :(得分:1)

是的,您可以绑定到List的索引。您只需在绑定表达式中使用常规索引语法;例如:

<TextBox DataContext="{Binding MyList[3]}" />
相关问题