Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a (static) Dictionary to Labels?

I have a static Dictionary

class X { static Dictionary<string,string> MyDict {get { ... }} }

This Dictionary contains Data i want to show in a Grid-Control:

<Grid>
  <!-- Row and Column-Definitions here -->
  <Label Grid.Row="0" Grid.Column="0" Content="{Binding MyDict.Key=="foo" }" ToolTip="foo" />
  <!-- some more labels -->
</Grid>

1.) i dont know how to get access (in xaml) to the dictionary

2.) i want to bind the Value of a specified key to the Content-Property of the Label.

how to do this?

like image 212
0xDEADBEEF Avatar asked Dec 21 '22 03:12

0xDEADBEEF


1 Answers

To get access to the Dictionary, you have to do something like this (if your DataContext isn't already an instance of X):

<Grid>
    <Grid.DataContext>
        <X xmlns="clr-namespace:Your.Namespace" />
    </Grid.DataContext>
    <!-- other code here -->
</Grid>

To access the values in the dictionary, your binding has to look as follows:

<Label Content="{Binding MyDict[key]}" />
like image 58
Nuffin Avatar answered Dec 24 '22 02:12

Nuffin