0

I'm trying to bind to a single item from a collection but I need to be able to pass in a value from the element as the index. Below is a sample of what I'm trying to accomplish.

ViewModel

public Dictionary<string, string> ListOfString = new Dictionary<string, string>
{ 
    {"0", "TextToDisplay" }
};

View

<TextBox Tag="0" Text="{Binding ListOfString[Self.Tag]}" />

I'm not sure how to get the value of TextBox.Tag and pass it to ListOfString

Bacon
  • 783
  • 2
  • 11
  • 31
  • 1
    Why is `` unsuitable? How many of these TextBoxes do you have? Why not generate them in a `DataTemplate` in an `ItemsControl` bound to `ListOfString`? You could bind `Key` to `Tag` and `Value` to `Text`. Not saying you're wrong, just trying to understand what your goal is here. BTW `ListOfString` should have `{ get; set; }` if you want to bind it. – 15ee8f99-57ff-4f92-890c-b56153 Jan 12 '16 at 17:21
  • I tried using an ItemsControl to render all of my items but they're path controls all with different shapes. They still display on screen using the ItemsControl but none of the shapes are in the correct location. I think it has to do with them being in a Canvas. – Bacon Jan 12 '16 at 19:50
  • If a Canvas is the ItemsControl's ItemsPanel, then to position the items, you'll have to bind some kind of x and y properties to Canvas.Left and Canvas.Top, as in this answer: http://stackoverflow.com/a/1265419/424129 -- funny, I just learned that an hour or two ago! – 15ee8f99-57ff-4f92-890c-b56153 Jan 12 '16 at 19:52

1 Answers1

2

You could use a MultivalueConverter in which you will pass the ListOfStrings Dictionary and the Tag property of the TextBox like so:

  <Window.Resources>
    <ns:ValuFromDicConverter x:Key="ValuFromDicConverter"/>
</Window.Resources>
<Grid>
    <TextBox Tag="0" x:Name="tb">
        <TextBox.Text>
            <MultiBinding Converter="{StaticResource ValuFromDicConverter}">
                <Binding Path="ListOfString"/>
                <Binding ElementName="tb" Path="Tag"></Binding>
            </MultiBinding>
        </TextBox.Text>
    </TextBox>
</Grid>

the converter will simply get the corresponding value in the Dictionary:

 public class ValuFromDicConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null) return null;
        return (values[0] as Dictionary<string, string>)[values[1].ToString()];

    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

and don't forget to define your dictionary as a property and set the DataContext

    private Dictionary<string, string> _listOfString = new Dictionary<string, string>
    { 
        {"0", "TextToDisplay" }
    };


    public Dictionary<string, string> ListOfString
    {
        get
        {
            return _listOfString;
        }

        set
        {
            if (_listOfString.Equals(value))
            {
                return;
            }

            _listOfString = value;

        }
    }
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47