Binding ContextMenu DataContext works with TextBlock but not with TextBox - c#

I got this in my view, I need to be able to edit the name of the Skeleton items in the TreeView via right-click -> context menu -> rename. There is a slight problem:
When I use TextBlock it works fine (TextBlock is not editable) but the binding shuts down when I use TextBox. I tried several bindings and ancestor types, to no avail. Not sure if I should switch to TextBox at all.
<UserControl.Resources>
<Image x:Key="visibilityImage" Source="../Resources/visibility.png" Height="16" Width="16" />
<Image x:Key="visibilityOffImage" Source="../Resources/visibilityoff.png" Height="16" Width="16" />
<Style TargetType="{x:Type ToggleButton}" x:Key="visibilityButtonStyle">
<Setter Property="Content" Value="{DynamicResource visibilityImage}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource visibilityOffImage}" />
</Trigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type commonVM:GyroSuitViewModel}">
<views:GyroSuitView></views:GyroSuitView>
</DataTemplate>
<DataTemplate DataType="{x:Type commonVM:SkeletonViewModel}">
<views:SkeletonView></views:SkeletonView>
</DataTemplate>
</UserControl.Resources>
<Grid Name="Root">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical">
<Label Content="{Binding Title}" Margin="2"/>
<TreeView Name="SkeletonItems" ItemsSource="{Binding Skeletons}" commonExtensions:RightClickTreeViewSelectionExtension.IsEnabled="True">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Grid.ColumnSpan" Value="2" />
</Style>
</Style.Resources>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Records}">
<DockPanel LastChildFill="True">
<ToggleButton Background="Transparent" BorderThickness="0" Width="20" Height="20" IsChecked="{Binding IsVisible, Mode=TwoWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ChangeVisibilityCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding ChangeVisibilityCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Stretch="Fill" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="true">
<Setter Property="Source" Value="../Resources/visibility.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsVisible}" Value="false">
<Setter Property="Source" Value="../Resources/visibilityoff.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
<ToggleButton Visibility="{Binding RecordingButtonVisibility}" Background="Transparent" BorderThickness="0" Width="20" Height="20" IsChecked="{Binding IsRecording, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RecordCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding RecordCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Stretch="Fill" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsRecording}" Value="true">
<Setter Property="Source" Value="../Resources/stop.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsRecording}" Value="false">
<Setter Property="Source" Value="../Resources/record.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
<Button Visibility="{Binding DeleteButtonVisibility}" Name="DeleteButton" Height="20" BorderThickness="0" Command="{Binding DeleteCommand}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Image Source="../Resources/delete.png" Stretch="Fill"/>
</Button>
<xctk:ColorPicker SelectedColor="{Binding SkeletonColor, Converter={converter:DrawingColorToMediaColorConverter}}" BorderThickness="0" Margin="2" StandardColors="{Binding ElementName=Root, Path=DataContext.AvailableSkeletonColors}" Background="Transparent" Width="20" ShowAdvancedButton="False" ShowAvailableColors="False" ShowDropDownButton="False" ShowStandardColors="True" StandardColorsHeader="" />
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}">
<TextBlock.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Visibility="{Binding Path=ContextMenuVisibility}">
<MenuItem Header="Rename"/>
<MenuItem Header="Open Containing Folder" Command="{Binding OpenFolderCommand}"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<ContentPresenter Name="details" Content="{Binding SelectedItem, ElementName=SkeletonItems}" />
</StackPanel>
</ScrollViewer>
</Grid>
When I use the TextBox these are the errors in the output:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Root'. BindingExpression:Path=DataContext.AvailableSkeletonColors; DataItem=null; target element is 'ColorPicker' (Name=''); target property is 'StandardColors' (type 'ObservableCollection`1')
and
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'TextBox' (Name=''); target property is 'Tag' (type 'Object')
Thanks for every input!

Related

WPF EventTrigger not always firing

I have a WPF TreeView that I customized visually looking like this:
I defined an EventTrigger to react on clicks:
<TreeView>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding OpenPartListCommand}" CommandParameter="{Binding ElementName=PartsTreeView, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem" >
<Grid Margin="6,4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Name="Bd"
Background="{Binding Background}"
BorderBrush="Black"
BorderThickness="1.2"
CornerRadius="0"
MinHeight="35"
Padding="5"
SnapsToDevicePixels="True"
Margin="{Binding Path=Margin}" >
<Grid>
<Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
<Expander.Header>
<ContentPresenter ContentSource="Header"
Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=#VALUE-80}" />
</Expander.Header>
<ItemsPresenter />
</Expander>
<ContentPresenter Name="CntPres"
ContentSource="Header"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="Collapsed"
Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=#VALUE-80}"
/>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="TreeViewItem.HasItems" Value="false">
<Setter
TargetName="Exp"
Property="Visibility"
Value="Collapsed" />
<Setter
TargetName="CntPres"
Property="Visibility"
Value="Visible" />
</Trigger>
<Trigger Property="TreeViewItem.Name" Value="IsSpecial">
<Setter Property="Background" TargetName="Bd" Value="#FFC3AF"/>
</Trigger>
<Trigger Property="TreeViewItem.Name" Value="IsNotSpecial">
<Setter Property="Background" TargetName="Bd" Value="#8BADC5"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Bd" Property="Background" Value="#FFF0EA"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<ItemsPanelTemplate.Resources>
</ItemsPanelTemplate.Resources>
<StackPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
The problem is that clicking on the root element "Ausführungsvorschrift" only expands the tree but the event is not fired. It fires when clicking on sub elements though and it fires when clicking inside the border. But the header does not fire anything.
Try to add triggers for the Expanded and Collapsed events to the Expander element in the template:
<Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Expanded">
<i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
</i:EventTrigger>
<i:EventTrigger EventName="Collapsed">
<i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Expander.Header>
<ContentPresenter ContentSource="Header" />
</Expander.Header>
<ItemsPresenter />
</Expander>

ListBox contextMenu SubItem Click

I have ListBox with ContextMenu, I have configured MenuItem to works only if I click in selected item.
This is XAML code:
<ListBox x:Name="MessagesLb" Grid.Column="1" Margin="241,100,22.4,50" Grid.Row="1" BorderThickness="0" FontSize="14" FontWeight="SemiBold" ItemsSource="{Binding Items}" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />
<Separator/>
<MenuItem Header="Dettagli" />
</ContextMenu>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="0">
<Setter Property="ContextMenu" Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I tried to add Click event in my MenuItem but it doesn't work.
Example:
<MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />
private void MessagesLbCopySubMi_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Click event done");
}
How can I solve this?
Define the ContextMenu as a resource:
<ListBox x:Name="MessagesLb" Grid.Column="1" Margin="241,100,22.4,50" Grid.Row="1" BorderThickness="0" FontSize="14" FontWeight="SemiBold"
ItemsSource="{Binding Items}" SelectionMode="Extended">
<ListBox.Resources>
<ContextMenu x:Key="cm">
<MenuItem Header="Copia" Click="MessagesLbCopySubMi_Click" />
<Separator/>
<MenuItem Header="Dettagli" />
</ContextMenu>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource cm}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="0">
<Setter Property="ContextMenu" Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

WPF ListBoxItem detect IsMouseOver inside a custom item

i have a ListBox which I fill with custom items. I want to detect a MouseOver event from a ListBoxItem inside the item in order to change visibility of a button. I have checked most of the answers on StackOverflow, the following solution was what I was looking for, but it doesn't work.
This is a code snippet from my ContactsView:
<ListBox ScrollViewer.CanContentScroll="False" VerticalContentAlignment="Top" ScrollViewer.ScrollChanged="ListBox_OnScrollChanged" BorderThickness="0,0,0,0" Margin="0,0,0,0" Padding="0" BorderBrush="{StaticResource ResourceKey=PrimaryColor}" Name="ListBox" ItemsSource="{Binding ListBoxItemsSource}" HorizontalContentAlignment="Stretch">
<i:Interaction.Triggers>
<events:RoutedEventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
<i:InvokeCommandAction Command="{Binding Path=ListBoxScrollChangedCommand}" />
</events:RoutedEventTrigger>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=ListBoxLoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Style.Triggers>
<Trigger Property="ListBoxItem.IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource PrimaryColor}"/>
</Trigger>
<Trigger Property="ListBoxItem.IsMouseOver" Value="False">
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<!-- Custom item -->
<items:ItemCorporateContact Value="{Binding Path=., Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<Separator Height="1" Margin="0" Background="#ececec" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And I have been trying to detect the event this way (code from a custom item that I add to a ListBox):
<Button Name="StartCallButton" VerticalAlignment="Center" Background="Red" Margin="10" HorizontalAlignment="Left">
<Button.Content>
<Image Source="{StaticResource PhoneIconBitmap}"></Image>
</Button.Content>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Any help will be greatly appreciated.
I had been searching for the same thing. Although the answer is provided in the question, but to specify the answer more clearly, following is the code that works for me.
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ViewTypeStackPanel" Orientation="Horizontal">
<Border BorderThickness="2,0,0,0" Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverterInstance}}" BorderBrush="Blue"/>
<Image Height="32" Width="32">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="{Binding Path=ImagePath}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsMouseOver}" Value="True">
<Setter Property="Source" Value="{Binding Path=ImagePathHover}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>

ContextMenu as staticresource issues

I've been scratching my head for a while at an issue i was having, eventually tracked it down to a problem with contextmenu's as a static resource.
The problem i'm having is that when i moved the contextmenu into a static resource, and used datatriggers to load a context menu based on the state of an object, the button commands seemed to get stuck on the datacontext bound on the first load, but the other binding's worked fine.
What i have is the following, a view which contains a listview using a custom item template, this item template is:
<DataTemplate x:Key="appListTemplate">
<Button cal:Message.Attach="Run" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#BBF7F7F7"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Height="50" HorizontalAlignment="Stretch" Background="{TemplateBinding Background}">
<Border CornerRadius="5" Height="35" Width="35" Background="SkyBlue" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"/>
<Label Content="{Binding AppName}" ToolTip="{Binding AppStatus}" VerticalAlignment="Center" Margin="50,0,0,0" FontSize="16"/>
<Image Source="{Binding Icon}" ToolTip="{Binding AppIconName}" Stretch="UniformToFill" Height="20" Width="20" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,15,0"/>
<StackPanel VerticalAlignment="Top">
<Line Stroke="#FFCDCDCD" Stretch="Fill" X2="1" />
<Line Stroke="#FFEAEAEA" Stretch="Fill" X2="1" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding AppStatus}" Value="{x:Static enum:AppStatus.DISABLED}">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="Lime"/>
</DataTrigger>
<DataTrigger Binding="{Binding AppStatus}" Value="{x:Static enum:AppStatus.NOACCESS}">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="ContextMenu" Value="{StaticResource ResourceKey=NoAccessContextMenu}" />
</DataTrigger>
<DataTrigger Binding="{Binding AppStatus}" Value="{x:Static enum:AppStatus.INSTALLED}">
<Setter Property="ContextMenu" Value="{StaticResource ResourceKey=InstalledContextMenu}"/>
</DataTrigger>
<DataTrigger Binding="{Binding AppStatus}" Value="{x:Static enum:AppStatus.NOTINSTALLED}">
<Setter Property="ContextMenu" Value="{StaticResource ResourceKey=UninstalledContextMenu}"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFF7F7F7"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
As you can see, depending on the status there is a different contextmenu loaded. these are defined as:
<ContextMenu x:Key="InstalledContextMenu" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="RunApplication" Header="Run" cal:Message.Attach="Run"/>
<MenuItem Header="Uninstall" cal:Message.Attach="Uninstall"/>
</ContextMenu>
<ContextMenu x:Key="UninstalledContextMenu" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="Install" Header="Install" cal:Message.Attach="Install"/>
</ContextMenu>
<ContextMenu x:Key="NoAccessContextMenu" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="RequestAccess" Header="Request Access" cal:Message.Attach="RequestAccess"/>
</ContextMenu>
The problem comes when you right click the first item in the list, it displays the options correctly E.G
SHIPPING
--------
Run
Uninstall
This work's 100%, however when you then right click on the second item in the list you get the following:
PROJECTS
--------
Run
Uninstall
as you can see, the appname binding updates fine, however when you click uninstall, it calls the uninstall method on the shipping VM, when it should be calling the uninstall on the projects VM. This will continue to happen with every item calling the methods on Shipping VM instead.
I'm at a total loss on what i can do about this, or even why it's happening. Any help would be most welcome.
Ok, Thanks to some hints from people, i've eventually tracked this down.
The issue is caused because the contextmenu is not the in visual tree, and microsofts "fix" is to make the contextmenu inherit it's parents datacontext, however this only ever happens once, and obviously when changing the datacontext, such as when changing selected item in a listview, the datacontext is never updated.
To solve this, i changed the contextmenu to the following.
<ContextMenu x:Key="InstalledContextMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="RunApplication" Header="Run" cal:Action.TargetWithoutContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" cal:Message.Attach="Run"/>
<MenuItem Header="Uninstall" cal:Action.TargetWithoutContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" cal:Message.Attach="Uninstall"/>
</ContextMenu>
<ContextMenu x:Key="UninstalledContextMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="Install" cal:Action.TargetWithoutContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" Header="Install" cal:Message.Attach="Run"/>
</ContextMenu>
<ContextMenu x:Key="NoAccessContextMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" StaysOpen="False">
<MenuItem Header="{Binding AppName}" HorizontalContentAlignment="Right" FontWeight="Bold" IsEnabled="False"/>
<Separator/>
<MenuItem x:Name="RequestAccess" cal:Action.TargetWithoutContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" Header="Request Access" cal:Message.Attach="RequestAccess"/>
</ContextMenu>
As you can see, i now create my own binding for the datacontext and then setup the target context for my caliburn micro actions on each menuitem. It's a bit of an arse having to do this for each menu item, but seems to be the simplest fix.

TargetName not found

I have two stackpanels on in another, the inner one has visibility "collapsed", it has the name "verborgen.
When mouseOver the inner stackpanel needs to change to visibility "visible".
So i use TargetName="verborgen"
However, it always returns to me that "The name "verborgen" is not recognized
<DataTemplate x:Key="WastebinTemplate">
<ContentControl map:MapLayer.Position="{Binding GeoLocation}">
<ContentControl.Content>
<StackPanel>
<TextBlock Background="{StaticResource Blue}" Text="{Binding Barcode}"/>
<StackPanel x:Name="verborgen" Visibility="Collapsed" Background="{StaticResource Orange}">
<Button Name="btnRemove" Content="Remove wastebin" Click="btnRemove_Click">
</Button>
<Label>Adres</Label>
<TextBox Name="txbAdres" Text="{Binding Address}"/>
<Label>Location type</Label>
<ComboBox ItemsSource="{Binding Source={StaticResource LocationTypeInstance}, Path=LocationTypes}"
DisplayMemberPath="Description" SelectedItem="{Binding LocationType}" SelectedValuePath="ID" SelectedValue="{Binding LocationType.ID}" />
<Label>Capaciteit</Label>
<Slider Minimum="0" Maximum="100" TickFrequency="10" Value="{Binding Capacity}"></Slider>
</StackPanel>
</StackPanel>
</ContentControl.Content>
</ContentControl>
<DataTemplate.Resources>
<Style TargetType="StackPanel">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="verborgen" />
<Setter Property="Panel.ZIndex" Value="9999"/>
</Trigger>
</Style.Triggers>
</Style>
</DataTemplate.Resources>
</DataTemplate>
Found it, the problem was instead of a datatemplate style I just used a datatemplate.trigger
<Window x:Class="Oefening2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:model="clr-namespace:Oefening2.model"
Title="Wastebins in Kortrijk" WindowState="Maximized">
<Window.Resources>
<model:LocationType x:Key="LocationTypeInstance"/>
<DataTemplate x:Key="WastebinTemplate">
<ContentControl map:MapLayer.Position="{Binding GeoLocation}">
<ContentControl.Content>
<StackPanel>
<TextBlock Background="{StaticResource Blue}" Text="{Binding Barcode}"/>
<StackPanel x:Name="verborgen" Visibility="Collapsed" Background="{StaticResource Orange}">
<Button Name="btnRemove" Content="Remove wastebin" Click="btnRemove_Click">
</Button>
<Label>Adres</Label>
<TextBox Name="txbAdres" Text="{Binding Address}"/>
<Label>Location type</Label>
<ComboBox ItemsSource="{Binding Source={StaticResource LocationTypeInstance}, Path=LocationTypes}"
DisplayMemberPath="Description" SelectedItem="{Binding LocationType}" SelectedValuePath="ID" SelectedValue="{Binding LocationType.ID}" />
<Label>Capaciteit</Label>
<Slider Minimum="0" Maximum="100" TickFrequency="10" Value="{Binding Capacity}"></Slider>
</StackPanel>
</StackPanel>
</ContentControl.Content>
</ContentControl>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="verborgen" Property="Visibility" Value="Visible"/>
<Setter Property="Panel.ZIndex" Value="9999" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid x:Name="Root">
<map:Map x:Name="myMap" CredentialsProvider="AnlnVG80DKkOfS5KLoUofPfy8t0a6AdtJvynRT_rxvV8MQ6cy6eEhQ6MHPBd5P3c" ZoomLevel="14" Center="50.8333,3.2667" MouseDoubleClick="MapWithPushpins_MouseDoubleClick">
<map:MapItemsControl ItemsSource="{Binding Wastebins}" ItemTemplate="{StaticResource WastebinTemplate}" />
</map:Map>
</Grid>
</Window>

Categories

Resources