I create a User Control. this has a dependencypoperty by this type
List<CustomSubMenuItem>
and CustomSubMeuItem
class CustomSubMenuItem
{
public string Title {get;set;}
public Color BackColor {get;set;}
publiv Visibility ItemVisibility {get;set;}
public ICommand Command {get;set;}
}
in XAML i bind to the prperty in usuall. but i cant make this items in XAML, Like ContextMenu or ListBoxItems.
<MyControl>
<MyControl.Items>
<CustomSubMenuItem Title="First" Visibility="{Binding Model.firstvisibility}"/>
<CustomSubMenuItem Title="Second" Visibility="{Binding Model.secondvisibility}"/>
</MyControl.Items>
</MyControl>
but this kind has error,what can I do.
UPDATE: Thanks, I Reach to answer below. i did not define the namespace of class in the xaml. and the upper code is true when i add the namespace: before CustomSubItemMenu.
Set your control datacontext to your list and then bind ItemsSource to it:
Then in XAML f.e:
You can bind your Title and Color the way you want it.
UPDATE:
If you want to bind Visibility to one of your property, one way to do it is to have bool value in Model and bind it to visibility. Also you need a ValueConverter to set true value as visible and false as hidden.
First, add a namespace in window. Declare that namespace where your ValueConverter class is defined.
XAML for binding visibility:
Then add ValueConverter to your :
Lastly, you need to create the ValueConverter class, use mine as a example: