views:

154

answers:

1

I have a Dictionary object in my ViewModel with key/values that translate words on the View.

It is possible to get the language information as an XML object and then pick out the translated phrase with XPath, something like this:

<TextBlock DataContext="{TranslatorDictionaryXml}" Text="{Binding XPath=/terms/term[key='edit']/value[@lang='en-US']}"/>

But is there a similar way to do this with a non-XML object which offers some kind of XPath-like syntax, e.g.

PSEUDO-CODE:

<TextBlock DataContext="{CurrentLanguageTranslatorDictionary}" Text="{Binding path=Key['edit']}"/>

I don't want to bind a collection to a ListView or any other collection element, but want to bind the one Translator object to individual TextBlocks and TextBoxes and ToolTips, etc. and then use some kind of path syntax to get a particular item out of the bound collection.

Is this possible?

+1  A: 

Yes, you can do both, there's an XPath property on a Binding as well. There's some good examples of how to do it here, and throughout the binding how-to samples. You can also use a collection's indexer to do it, but it doesn't need single quotes or escaped quotes.

<TextBox Text="{Binding Path=Countries[US]}" />

public partial class Window1 : Window
{
 public Window1()
 {
  InitializeComponent();

  Countries = new Dictionary<string, string>();
  Countries.Add("US", "United States");
  Countries.Add("CA", "Canada");

  this.DataContext = this;
 }

 public Dictionary<string, string> Countries { get; set; }
}
rmoore
perfect, thanks!
Edward Tanguay