Question

Is it possible to set a datacontext of a textbox to a particular index of a List>?

I have a list that I am using for a data context of a Chart and I have been able to access the data with code on textchanged with this:

        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();
        }

But if possible I'd like to just use a data context.

Edit to add my current classes:

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));
    }
}
Was it helpful?

Solution

Yes, you can bind to an index of a List. You just use the normal indexing syntax within the binding expression; for example:

<TextBox DataContext="{Binding MyList[3]}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top