I made a style for my menu control and now I want to use that style for all the menuItems, but with different text in the textbox. I was wondering if I can use a List to populate the binding element…I tried but it doesnt work…Have I missed something or I have to use something else?
List<string> itemArray = new List<string>();
itemArray.Add("label1");
itemArray.Add("label2");
itemArray.Add("label3");
Binding binding = new Binding();
binding.Path = new PropertyPath("itemArray");
this.menu1.SetBinding(TextBox.TextProperty, binding);
and the one part of the style is, if it helps…:
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Grid>
<Border Name="MainBorder" BorderThickness="2,2,2,0" >
<Grid>
<TextBlock Text="{Binding Path=itemArray}" Margin="30,10,0,0" FontFamily="Arial" FontSize="14" FontWeight="Bold" />
<Image Width="15" Height="15" Source="image.PNG" Margin="-100,0,0,0" />
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
You’re trying to bind a text element to a
List<T>, which will result in the type name. If you want the menu to populate itself from a list of objects, consider binding the menu’sItemsSourceproperty to that list:In this example, each list item is an object with a
Textproperty which shows up as the display string, and aCommandproperty, which is an object that implementsICommand. When the user chooses a menu item, that list item’sCommand.Executemethod is invoked; you could use something likeRelayCommandorReactiveCommandto turn that into a method call.This allows for a flat menu; for a hierarchical menu you’ll have to do something a little different.