Context Menu Left Click - wpf

I have a context menu which used to be a pop up menu. When I first load the program the context menu is empty as the menu items are bound to a list. However if I load the program and right click first the list is fine and then I can go to left click and the list appears. I'll show images to help my description:
Left click first
Right click first and also left click after
Can anyone help me understand why this has happened and how to fix it?
Edit - Xaml
<DataTemplate x:Key="AddNodeTemplate">
<StackPanel>
<Button x:Name="buttonAdd">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="0">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
<Canvas>
<Image Source="Images/bkg_plus.png" Width="30" Height="30" Panel.ZIndex="5"/>
<Rectangle Stroke="LightGray" StrokeDashArray="2 2" Width="120" Height="30" VerticalAlignment="Center" Margin="0,0,0,0"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
<Rectangle.Fill>
<SolidColorBrush Color="LightGray"/>
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="Add Property" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="30 10 0 0" Background="Transparent"/>
</Canvas>
<Button.ToolTip>
<ToolTip>
<Label Content="Add Property"/>
</ToolTip>
</Button.ToolTip>
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding AvailableProperties}">
<ContextMenu.Resources>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Path=Name}"/>
<Setter Property="ToolTip" Value="{Binding Path=ToolTips}"/>
<Setter Property="Command" Value="{Binding Path=Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
<!--<Setter Property="Icon" Value="{Binding Icon , Converter={StaticResource ImageToSourceConverter}"/>-->
</Style>
</ContextMenu.Resources>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
New Result
Ok so this is now what happens when I do left click first
So I have tried using a pop up and have a menu inside this and this is my XAML for the result, the only problem now is I want to change the highlighted background when the mouse is hovered over the "Add Existing Property" does anyone know how to do that and also move the menu over to the right I'll post a picture up to explain
<DataTemplate x:Key="AddNodeTemplate">
<Border BorderThickness="1" Background="#F7F7F7">
<Border.BorderBrush>
<DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#F7F7F7">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50"/>
<RectangleGeometry Rect="50,50,50,50"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<StackPanel>
<ToggleButton Height="30" Width="120" Style="{StaticResource ChromelessToggleButton}" x:Name="toggleButtonAdd" IsHitTestVisible="{Binding ElementName=Popup, Path=IsOpen, Mode=OneWay, Converter={StaticResource OppositeBooleanConverter}}">
<ToggleButton.ToolTip>
<ToolTip>
<Label Content="Add Property"/>
</ToolTip>
</ToggleButton.ToolTip>
</ToggleButton>
<Popup IsOpen="{Binding IsChecked, ElementName=toggleButtonAdd}" x:Name="Popup" StaysOpen="False" Placement="Right">
<Border BorderBrush="Black" BorderThickness="0" Background="#F7F7F7">
<StackPanel Margin="5,10,5,5" Orientation="Horizontal">
<Menu Background="#F7F7F7">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Add Exisiting Properties">
<ListBox BorderBrush="Black" BorderThickness="0" Background="Transparent" Margin="5" Padding="4" Width="130"
ItemsSource="{Binding Path=AvailableProperties}" SelectionChanged="Selector_OnSelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#5194C7"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon, Converter={StaticResource ImageToSourceConverter}}" Width="12" Height="12" Margin="3" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</MenuItem>
<MenuItem Header="Upscale Well logs">
</MenuItem>
<MenuItem Header="Upscale well_top attributes">
</MenuItem>
<MenuItem Header="Upscale point attributes">
</MenuItem>
<MenuItem Header="Calculate">
</MenuItem>
</Menu>
</StackPanel>
</Border>
</Popup>
</StackPanel>
</Border>
</DataTemplate>
Picture of menu right under the popup rather than to the right like the above pictures I want the background to be blue rather than the gray background as in the picture below

You can do this by adding a Context Menu and Style like below, I dont think its necessary to go with ListBox inside MenuItem
<Button.ContextMenu>
<ContextMenu ItemsSource="{Binding AvailableProperties}">
<ContextMenu.Resources>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Path=Name}"/>
<Setter Property="ToolTip" Value="{Binding Path=ToolTips}"/>
<Setter Property="Command" Value="{Binding Path=Command}"/>
<Setter Property="CommandParameter" Value="{Binding Path=CommandParameter}"/>
<Setter Property="Icon" Value="{Binding Icon , Converter={StaticResource ImageToSourceConverter}"/>
</Style>
</ContextMenu.Resources>
</ContextMenu>
</Button.ContextMenu>

Related

Round close button in corner of image

I am creating gallery, it contains images. So I want to remove the images from gallery using close buttons. The position of the close button should be the corner of the every images. How can I create like this corner buttons styles? Any idea?
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="120">
<Button Height="25" VerticalAlignment="Top" Content="Show" Width="100" Margin="5" />
<Image Height="90" Source="{Binding Path=Thumbnail}" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
<TextBlock TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Text="{Binding Path=Name}" >
<TextBlock.ToolTip>
<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
<ToolTip.Content>
<TextBlock Text="{Binding Name}"/>
</ToolTip.Content>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
<!--<Separator Width="5" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Background="Transparent"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
place Image and Button in the same Grid. Align button to top right corner, and add margin to top and right sides of image, so the button goes outside image borders
<Grid>
<Image Margin="0,8,8,0"/>
<Button HorizontalAlignment="Right" VerticalAlignment="Top"
Width="16" Height="16" Content="x"/>
</Grid>
Here's a round button from one of my samples which looks pretty similar to what you're after.
The colours and mouseover may be different.
<SolidColorBrush x:Key="DarkLight" Color="Chocolate"/>
<SolidColorBrush x:Key="BrightMid" Color="PapayaWhip"/>
<Geometry x:Key="CloseIcon">
M19.85228,12.08996L12.093,19.849201 24.242323,31.997846 12.094,44.145998 19.852051,51.904958 32.001186,39.756277 44.150543,51.904958 51.909,44.145994 39.760246,31.997501 51.909,19.849201 44.15049,12.08996 32.001431,24.238849z M32,0C49.671021,3.1599484E-07 64,14.329407 64,31.998501 64,49.677606 49.671021,63.997003 32,63.997003 14.328003,63.997003 0,49.677606 0,31.998501 0,14.329407 14.328003,3.1599484E-07 32,0z
</Geometry>
<Style x:Key="CloseButton" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="{StaticResource BrightMid}"/>
<Setter Property="Background" Value="{StaticResource DarkLight}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
<Border CornerRadius="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"
Width="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"
Name="border"
BorderThickness="1"
BorderBrush="Black"
Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"
>
<Path Data="{StaticResource CloseIcon}" Stretch="Fill"
Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"
/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

WPF TabItem - Image Binding - using Triggers

I want to show Image inside of the Title of header in TabControl
This question(WPF TabItem - Image Binding) seems is right, but I have using Triggers and it doesn't help on my code.
Here is what I have Providing:
<TabControl Name="MainTabControl" Margin="0,28,0,0" TabStripPlacement="Top" >
<TabControl.Resources>
<Image x:Key="imgProfile" Source="/Icons/red icons/profile.png" />
<Image x:Key="imgComment" Source="Icons/red icons/notification.png" />
<Style TargetType="TabItem" >
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding}"
MouseDown="UIElement_OnMouseDown"
FontSize="18"
/>
<Image Height="25" Margin="4,0,4,0"
Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag.Source}" />
</WrapPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="Border" BorderThickness="1,1,1,0" BorderBrush="Gainsboro" CornerRadius="0" Margin="2,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="5,2"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="BorderBrush" Value="Transparent" />
<Setter TargetName="Border" Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource TabItemNormal}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Name="tiProfile" Header="a" Tag="{StaticResource imgProfile}"></TabItem>
<TabItem Name="tiComment" Header="b" Tag="{StaticResource imgComment}"></TabItem>
</TabControl>
the binding from TabItem.HeaderTemplate to the TabItem.Tag must be via FindAncestor of TabItem type.
instead create Images in Resources, create BitmapImage, and bind it the Image.Source.
so, Resources:
<BitmapImage x:Key="imgProfile" Source="/Icons/red icons/profile.png" />
<BitmapImage x:Key="imgComment" Source="Icons/red icons/notification.png" />
Header DataTemplate:
<Image Height="25" Margin="4,0,4,0"
Source="{Binding Tag, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" />

MVVM Treeview Click Node

I am using an existed MVVM project and I did fine until now. I have a treeView code and I want
to release the node when it pressed. in other words, today if I click on any node, Its not selectable anymore (until you click another node). I want to be able to click thesame node as many times as I want. If I double click node , I have "IsExpended" property and it goes to this code section. I cant see where is the doubleclick bind, single click binding...
Here's the xaml
<UserControl x:Class="Envitech.Setup.Presentation.Views.TreeViewViews.StationTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:behaviour="clr-namespace:Envitech.Setup.Presentation.Extensions;assembly=Envitech.Setup.Presentation" xmlns:nodes="clr-namespace:Envitech.Setup.Presentation.ViewModels.TreeViewViewModels.Nodes" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="226" d:DesignWidth="227" xmlns:ml="clr-namespace:Envitech.Setup.Presentation.ml_resources">
<TreeView Margin="10 0 10 20" ItemsSource="{Binding Path=Root.Children}" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="Padding" Value="1,0,0,0" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="behaviour:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="19" Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ToggleButton x:Name="Expander" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
<Border Grid.Column="1" x:Name="Selection_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" ContentSource="Header" />
</Border>
<ItemsPresenter Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" x:Name="ItemsHost" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter Property="Visibility" Value="Collapsed" TargetName="ItemsHost" />
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="Visibility" Value="Hidden" TargetName="Expander" />
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TargetName="Selection_Border" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TargetName="Selection_Border" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Style for the ToggleButton control used to expand/collapse a TreeViewItem control -->
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Focusable" Value="False" />
<Setter Property="Width" Value="19" />
<Setter Property="Height" Value="13" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Width="19" Height="13" Background="#00FFFFFF" x:Name="Border">
<Border Width="9" Height="9" x:Name="Border1" SnapsToDevicePixels="True" BorderBrush="#FF9495A2" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#FFFFFFFF" Offset="0.4" />
<GradientStop Color="#FFC6CEDA" Offset="1" />
</LinearGradientBrush>
</Border.Background>
<Path Fill="#FF000000" Margin="1,1,1,1" x:Name="ExpandPath" Data="M0,2L0,3 2,3 2,5 3,5 3,3 5,3 5,2 3,2 3,0 2,0 2,2z" />
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" Value="M0,2L0,3 5,3 5,2z" TargetName="ExpandPath" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type nodes:MonitorNode}">
<StackPanel Orientation="Horizontal" Margin="0 2 0 0">
<Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" />
<TextBlock Text="{x:Static ml:MultiLang._157}" x:Name="ML_0048" />
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{x:Static ml:MultiLang._158}" x:Name="ML_0050" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type nodes:StationNode}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Margin="0 2 0 0">
<StackPanel.ContextMenu>
<ContextMenu HasDropShadow="True">
<MenuItem Header="{x:Static ml:MultiLang._160}" Command="{Binding CopyMonitorsCommand}" x:Name="ML_0055" />
<MenuItem Header="{x:Static ml:MultiLang._159}" Command="{Binding PasteMonitorsCommand}" x:Name="ML_0056" />
<MenuItem Header="{x:Static ml:MultiLang._162}" Command="{Binding CopyStationsCommand}" x:Name="ML_0057" />
<MenuItem Header="{x:Static ml:MultiLang._161}" Command="{Binding PasteStationsCommand}" x:Name="ML_0058" />
</ContextMenu>
</StackPanel.ContextMenu>
<Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" />
<TextBlock Text="{x:Static ml:MultiLang._157}" x:Name="ML_0060" />
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{x:Static ml:MultiLang._158}" x:Name="ML_0062" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type nodes:FilteringNode}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Margin="0 2 0 0">
<Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</UserControl>
Thank you
When you double click a tree view item, it will be expanded. When it is expanded, your IsExpanded property will be changed due to your binding. Single clicking something that is NOT currently selected will change your IsSelected property. However, clicking it again (not double clicking) will NOT change the IsSelected property. Seems like what you want to do is to also add a 'Click' event handler. This way, each time it is clicked, you will be notified, not just when it is clicked for the first time when something else was selected.
To Accomplish this, add an for the 'MouseDoubleClick' event to your TreeViewItem style. If you want a single click, just do the same for the 'MouseDown' event:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
<EventSetter Event="MouseDoubleClick" Handler="DoubleClickedItem"/>
</Style>
Once your XAML has the code above in it, right click your mouse on the word 'DoubleClickedItem' and select the 'NavigateToHandler' option and Visual Studio will generate a method stub for you in your code behind. You can then implement your double click behavior there.
This answer explains how to use the Interactivity extensions to remain fully MVVM.
WPF RightClick MouseBinding on release?
I've not been able to use the MouseLeftButtonDown event but MouseLeftButtonUp worked in my case (notification of a click on an already selected TreeViewItem).
I'm guessing that MouseLeftButtonDown is handled earlier in the event tree, so we never get to see it.

Why can't I use AlternationCount on a Border?

In the code below if I change
<Style TargetType="{x:Type Border}">
To
<Style TargetType="{x:Type ListBoxItem}">
It will color my entire listboxitem the correct color. But I only want to hit the border background
<ListBox x:Name="FilteredMessagesListBox" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" SelectionMode="Extended" Background="Transparent" AlternationCount="2">
<ListBox.Resources>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightBlue"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGreen"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate >
<DataTemplate>
<DockPanel Margin="0,0,0,3">
<Button x:Name="AttachmentImageButton" Click="AttachmentImageButton_Click" DockPanel.Dock="Bottom" MaxWidth="200" MaxHeight="200" HorizontalContentAlignment="Center" Visibility="{Binding ElementName=AttachmentImageButton, Converter={StaticResource cImageAttachmentToVisible}}" >
<Image Source="{Binding Path=Attachment}" x:Name="AttachmentImage" />
</Button>
<Button x:Name="AttachmentButton" Click="AttachmentButton_Click" DockPanel.Dock="Right" Visibility="{Binding ElementName=AttachmentButton, Converter={StaticResource cAttachmentToVisible}}" >
<Image Source="/MobilWPF;component/Resources/Images/PaperClip/PaperClip.jpg" Width="20" Height="20"/>
</Button>
<TextBlock Text="{Binding Converter={StaticResource cGetInstantMessageHeader}}" Width="120" TextWrapping="Wrap" HorizontalAlignment="Left" Background="Transparent" FontSize="10" DockPanel.Dock="Left"/>
<Border DockPanel.Dock="Left" HorizontalAlignment="Stretch" CornerRadius="5">
<DockPanel>
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
</DockPanel>
</Border>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I tried this:
<Border DockPanel.Dock="Left" HorizontalAlignment="Stretch" CornerRadius="5">
<Border.Triggers>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
<Setter Property="Background" Value="LightBlue"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="1">
<Setter Property="Background" Value="LightGreen"/>
</DataTrigger>
</Border.Triggers>
<DockPanel>
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
</DockPanel>
</Border>
Cannot find the static member 'BackgroundProperty' on the type 'ContentPresenter'
The problem is that AlternationIndex is set on the containers (ListBoxItems in this case), not on some child within them. You could solve this by using DataTriggers that bind to AlternationIndex on the parent ListBoxItem instead:
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
<Setter Property="Background" Value="LightBlue"/>
</DataTrigger>

What's wrong with this ContentTemplate?

I'm getting an error for this content template within a style: "Must specify both Property and Value for Setter." Aren't I doing that?
<Style x:Key="LinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Label x:Name="ContentRoot">
<StackPanel Orientation="Horizontal">
<Viewbox Width="24" Height="24" VerticalAlignment="Center">
<Image x:Name="ButtonImage" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Tag}" />
</Viewbox>
<TextBlock VerticalAlignment="Center" x:Name="ButtonText" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Content}"></TextBlock>
</StackPanel>
</Label>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonText" Property="TextBlock.TextDecorations" Value="Underline"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
And here is a button that will be using this style:
<Button Name="HelpButton" Style="{StaticResource LinkButton}" Height="30" Content="Help" Tag="Help.png"/>
Thanks!
I have loaded this up and have no such problems. The only problem I see is that your button will never have the style applied. This is because if you want the style applied you need to remove the x:Key from the style. Otherwise if you want the style applied only to the HelpButton, the definition should look like this:
<Button Name="HelpButton" Style="{StaticResource LinkButton}" Height="30" Content="Help" Tag="Help.png"/>
But I cannot see the error you are seeing.

Resources