My goal is to disable/collapse a menuitem if the user does not have the rights to it. The binding i'm looking for is in my ViewModel as a boolean. I know that it's trying to find the property "IsAdmin" inside of the property "InstructionsUpdates". I just can't find a way to make the menuitem use the datacontext of the window.
Viewmodel:
Public Property IsAdmin As Boolean
Get
Return _IsAdmin
End Get
Set
SetValue(_IsAdmin, Value)
End Set
End Property
Public Property InstructionsUpdates As ObservableCollection(Of ClsInstruction)
Get
Return _InstructionsUpdates
End Get
Set
SetValue(_InstructionsUpdates, Value)
End Set
End Property
Public Class ClsInstruction
Public FileName As String = ""
Public Property Name As String = ""
Public Property IsChecked As Boolean = False
Sub New(nName, nFileName)
Name = nName
FileName = nFileName
End Sub
End Class
Public Sub New()
IsAdmin = False
End Sub
XAML:
<ItemsControl x:Name="InstructionsItemControl" ItemsSource="{Binding InstructionsUpdates}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" ScrollViewer.CanContentScroll="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem x:Name="MnuViewInstruction"
Click="MnuViewInstruction_Click"
Header="Preview">
<MenuItem.Icon>
<iconPacks:PackIconMaterialDesign Kind="Pageview"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem x:Name="MnuEdit"
Click="MnuEdit_Click"
Header="Edit (Binding not working)"
IsEnabled="{Binding IsAdmin}">
<MenuItem.Icon>
<iconPacks:PackIconMaterial Kind="FileEdit" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
<CheckBox IsChecked="{Binding IsChecked}"/>
<Label Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I've tried:
IsEnabled="{Binding IsAdmin, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
And:
IsEnabled="{Binding DataContext.IsAdmin, ElementName=MainWindow1}">
"MainWindow1" being the x:Name I gave to my window to test it out I've also tried using a window.ressource and then putting it inside my stackpanel, but turns out bindings don't work inside a ressource (form what I've seen)