yoav.str
yoav.str

Reputation: 1542

wpf listbox dataBinding

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:

  1. where I putted ** I want the textblock text to contain the Size key value
  2. how can I do command pattern when a button is pushed ?

Upvotes: 0

Views: 892

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292355

Inside the ItemTemplate, the DataContext is an item from the source collection, so in that case it's a KeyValuePair<string, int>. So the path to the key is just "Key" :

<TextBlock x:Name="textBlock" Text="{Binding path=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 ICommand property exposed by your ViewModel. However in your case there is not ViewModel, since your data object is a KeyValuePair<string, int>... Please give more details if you want a more complete answer

Upvotes: 2

Related Questions