Reputation: 13397
I am trying to create a Wpf UserControl with a variable number of buttons.
In the code behind public string[] MenuItems { get; set; }
contains the Text (Content) of the buttons, so every item in the array should correspond to one button.
I tried:
<UserControl x:Class="MyApp.Controls.MenuButtons"
xmlns:m="clr-namespace:MyApp.Controls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<DockPanel LastChildFill="False"
Background="{StaticResource ControlBackground}"
DockPanel.Dock="Top"
Height="35"
Margin="5,0,0,0">
<ItemsControl ItemsSource="{Binding MenuItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"
Style="{StaticResource MenuButton}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
</UserControl>
Using the control in a window like this:
<c:MenuButtons MenuItems="..."></c:MenuButtons>
Gives the error:
The member "MenuItems" is not recognized or is not accessible.
MenuItems is sometimes recognized in xaml intellisense, sometimes not.
The window has iself as datacontext:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Upvotes: 0
Views: 3458
Reputation: 18580
Your MenuItems
property should be a DependancyProperty
in order to use it as Binding Target.
The type should be string[] not string.
static readonly DependencyProperty MenuItemsProperty = DependencyProperty.Register("MenuItems", typeof(string[]), typeof(MenuButtons), new PropertyMetadata(null));
Upvotes: 2