i have a listbox who takes values from Dictionary Size:
this is the Size type:
public Dictionary<string, int> Size
{
get;
private set;
}
this is my listbox
<ListBox x:Name="boardSize" ItemsSource="{Binding Size}" ItemTemplate="{DynamicResource DataTemplate1}" />
this is my the associated DataTemplate:
<Rectangle Margin="8,8,16,8" Stroke="Black" RadiusX="45" RadiusY="45">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFE24A4A" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock x:Name="textBlock" **Text="{Binding path=Size}"**/>
I have two problems:
- where I putted ** I want the textblock text to contain the Size key value
- how can I do command pattern when a button is pushed ?
Inside the
ItemTemplate, theDataContextis an item from the source collection, so in that case it’s aKeyValuePair<string, int>. So the path to the key is just “Key” :Your second question is not very clear, what do you want to do exactly ? Usually, binding to commands is used in MVVM: you bind to a
ICommandproperty exposed by your ViewModel. However in your case there is not ViewModel, since your data object is aKeyValuePair<string, int>… Please give more details if you want a more complete answer