set the content of togglebutton based on condition in xaml - wpf

I don't have that much experiences in WPF and XAML files, and maybe my question is very simple. so let me get your help if possible.
As a brief, it's a survey system that will go through some questions, where each question has exactly three answers. Answers are in a ListBox.
In order to make it easier for illiterate people, I want that the ToogleButton in each answer to have different shape: 1st is square, 2nd is Circle and 3rd is Triangle. my objective is to set the shape based on the ListBoxItem index.
Code is below with a static shape (Currently Triangle):
<ListBox x:Name="AnswersListBox" Margin="0 16 0 16" ItemsSource="{Binding Path=CurrentQuestion.Answers, ElementName=Questionctl}" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<Grid Height="85">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="300*"/>
</Grid.ColumnDefinitions>
<ToggleButton Margin="10 0" Style="{StaticResource MaterialDesignActionToggleButton}" ToolTip="Click to Select" VerticalAlignment="Center"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="32" Height="32">
<Canvas Width="32" Height="32">
<Path Data="M12,2L1,21H23M12,6L19.53,19H4.47" Width="32" Height="32" Stretch="Fill"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}, Path=Foreground}"/>
</Canvas>
</Viewbox>
</ToggleButton>
<TextBlock Text="{Binding Path=AnswerID, Converter={StaticResource AnswerTextConverter}}" VerticalAlignment="Center" TextWrapping="Wrap"
Foreground="#424242" Grid.Column="1" FontSize="24"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to change the property Data of the object Path based on the Main Container's index which is a ListBoxItem. Most probably, I have to use triggers to set the Data value based on the index number of the ListBoxItem, but unable to do that...
Is that possible?

In ListBox, there are no direct properties to get the index of the item. However, the same can be achieved by setting the AlternationCount and accessing AlternationIndex.
<ListBox x:Name="AnswersListBox"
Margin="0 16 0 16"
ItemsSource="{Binding Path=Answers}"
Grid.Row="1"
AlternationCount="100">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<Grid Height="85">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="300*"/>
</Grid.ColumnDefinitions>
<ToggleButton Margin="10 0" Style="{StaticResource MaterialDesignActionToggleButton}" ToolTip="Click to Select" VerticalAlignment="Center"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="32" Height="32">
<Canvas Width="32" Height="32">
<Path Width="32" Height="32" Stretch="Fill"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}, Path=Foreground}">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="1">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ItemsControl.AlternationIndex)}" Value="2">
<Setter Property="Data" Value="M12,2L1,21H23M12,6L19.53,19H4.47"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</Canvas>
</Viewbox>
</ToggleButton>
<TextBlock Text="{Binding Path=AnswerID, Converter={StaticResource AnswerTextConverter}}" VerticalAlignment="Center" TextWrapping="Wrap"
Foreground="#424242" Grid.Column="1" FontSize="24"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Replace the Value of Data with the required shape.

Many Thanks to #Subramaniam B, and based on his Answer which requires a few changes in the binding of the DataTrigger where it should be:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M28.3,28.3H0L14.2,0L28.3,28.3z M2.9,26.6h22.5L14.2,4L2.9,26.6z"/>
</DataTrigger>
and the full code will be:
<ListBox x:Name="AnswersListBox" AlternationCount="3">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type DAL:Answer}">
<ToggleButton Style="{StaticResource myCheckboxToggleButtonWithInsideCheck}" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=(ItemsControl.AlternationIndex)}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Viewbox Width="60" Height="60">
<Canvas Width="32" Height="32">
<Path Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}, Path=Foreground}" Height="32" Stretch="Fill" Width="32">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="0">
<Setter Property="Data" Value="M16.2,28.3h-4.1C0.5,28.3,0,27.9,0,16.2v-4.1C0,0.5,0.5,0,12.1,0h4.1c11.7,0,12.1,0.5,12.1,12.1v4.1C28.3,27.9,27.9,28.3,16.2,28.3z M12.1,1.9C7,1.9,4.1,1.9,3,3s-1.1,4-1.1,9.1v4.1c0,5.2,0,8,1.1,9.1c1.1,1.1,4,1.1,9.1,1.1h4.1c5.2,0,8,0,9.1-1.1c1.1-1.1,1.1-4,1.1-9.1v-4.1c0-5.2,0-8-1.1-9.1c-1.1-1.1-4-1.1-9.1-1.1H12.1z"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="1">
<Setter Property="Data" Value="M28.3,28.3H0L14.2,0L28.3,28.3z M2.9,26.6h22.5L14.2,4L2.9,26.6z"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=(ItemsControl.AlternationIndex)}" Value="2">
<Setter Property="Data" Value="M14.2,28.3C6.4,28.3,0,22,0,14.2S6.4,0,14.2,0s14.2,6.4,14.2,14.2S22,28.3,14.2,28.3z M14.2,2.2c-6.6,0-12,5.4-12,12c0,6.6,5.4,12,12,12c6.6,0,12-5.4,12-12C26.2,7.6,20.8,2.2,14.2,2.2z"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</Canvas>
</Viewbox>
</ToggleButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

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>

How to show / Hide based on isSelected property and another list item property?

I have a list of lvAccessPoints which has 3 properties,
1)name
2)password
3)isSecured
i a texbox and button in each item, which i am currently showing when an item is selected.
so now when i select one item text box and button will come up.
But i also want to check the property isSecured for showing textbox
condition is
textbox=> visible: if item is selected and has isSecured property true
hidden:if isSecured is false (irrespective of selected or not)
<ListView Margin="0" Name="lvAccessPoints" Background="#ff1d1d1d" Grid.Row="1" BorderThickness="0">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<!--<iconPacks:PackIconModern Kind="ConnectionWifi" Foreground="White" Width="30" Height="30"/>-->
<TextBlock Text="{Binding Points.Name}" FontWeight="Bold" Foreground="White" Padding="10,0" FontSize="15" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
<TextBox HorizontalAlignment="Left"
x:Name="txt"
TextWrapping="Wrap"
Text="{Binding Password}"
VerticalAlignment="Center"
Width="200"
Height="28"
Visibility="Collapsed" />
<Button x:Name="btn" Grid.Column="1" Visibility="Collapsed" Width="100" Margin="10" HorizontalAlignment="Left" Background="#FF2d89ef" Foreground="White" Padding="5" VerticalAlignment="Center" Grid.Row="2" Click="Button_Click_1">
<TextBlock Text="Connect" />
</Button>
</StackPanel>
</WrapPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="True">
<Setter TargetName="txt" Property="Visibility" Value="Visible" />
<Setter TargetName="btn" Property="Visibility" Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListView.ItemTemplate>
To show the text box i need to check 2 properties one is isSecured, and another is isSelectd.
How can i change my code?
Use a MultiDataTrigger:
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="True" />
<Condition Binding="{Binding Path=points.isSecured}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="txt" Property="Visibility" Value="Visible" />
<Setter TargetName="btn" Property="Visibility" Value="Visible" />
</MultiDataTrigger>
</DataTemplate.Triggers>

Wpf Popup loses focus but stays opened

I have the following Popup:
<Popup Name="popupBind"
AllowsTransparency="True"
Helpers:FocusHelper.IsFocused="{Binding RelativeSource={RelativeSource Self},
Path=IsOpen,
Mode=OneWay}"
HorizontalOffset="-30"
IsOpen="{Binding IsBindingBegun,
Mode=TwoWay}"
Placement="Bottom"
PlacementTarget="{Binding ElementName=bindButton}"
StaysOpen="False"
>
<Border Background="{DynamicResource {x:Static Styles:CommonStyles.ButtonsPanelBackgroundKey}}"
BorderBrush="Black"
BorderThickness="1"
Padding="0">
<StackPanel HorizontalAlignment="Stretch">
<StackPanel.Resources>
<TemplateSelectors:VersionRangeDataTemplateSelector x:Key="VersionRangeDataTemplateSelector" />
<Converters:RangeToVisibilityConverter x:Key="RangeToVisibilityConverter" />
</StackPanel.Resources>
<StackPanel Margin="5,5,5,2"
HorizontalAlignment="Stretch"
Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5" />
</Style>
</StackPanel.Resources>
<Button Command="{Binding BindCommand}"
>
</Button>
<Button Command="{Binding BindCommand}"
>
</Button>
<Button Command="{Binding BindCommand}"
>
</Button>
<Button Command="{Binding BindCommand}"
>
</Button>
</StackPanel>
<ItemsControl Margin="10,2,5,5"
Focusable="False"
Grid.IsSharedSizeScope="True"
ItemTemplateSelector="{StaticResource VersionRangeDataTemplateSelector}"
ItemsSource="{Binding Path=VersionsVm.TempRanges}">
<ItemsControl.Resources>
<Style TargetType="CheckBox">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Padding" Value="0" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Padding" Value="2" />
</Style>
<DataTemplate x:Key="TwoDistinctVersionsRangeTemplateKey">
<Grid Margin="2,4" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="_1" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_2" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_3" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_4" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_5" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_6" />
<ColumnDefinition Width="Auto" SharedSizeGroup="_7" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IncludeStartEdge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Grid.Column="1" Margin="2,0,5,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IncludeStartEdge}" Value="True">
<Setter Property="Text" Value="[" />
</DataTrigger>
<DataTrigger Binding="{Binding IncludeStartEdge}" Value="False">
<Setter Property="Text" Value="(" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Grid.Column="2"
HorizontalAlignment="Left"
Text="{Binding FromVersionName}"
TextAlignment="Left" />
<TextBlock Grid.Column="3"
Width="10"
Margin="5,0"
HorizontalAlignment="Center"
Text="-"
TextAlignment="Center" />
<TextBlock Grid.Column="4"
HorizontalAlignment="Left"
Text="{Binding ToVersionName}"
TextAlignment="Left" />
<TextBlock Grid.Column="5" Margin="5,0,2,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IncludeEndEdge}" Value="True">
<Setter Property="Text" Value="]" />
</DataTrigger>
<DataTrigger Binding="{Binding IncludeEndEdge}" Value="False">
<Setter Property="Text" Value=")" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<CheckBox Grid.Column="6" IsChecked="{Binding IncludeEndEdge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="FixedVersionRangeTemplateKey">
<StackPanel>
<TextBlock Margin="2,4"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{Binding FromVersionName}"
TextAlignment="Left" />
<StackPanel>
<StackPanel.Visibility>
<MultiBinding Converter="{StaticResource RangeToVisibilityConverter}">
<MultiBinding.Bindings>
<Binding />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
</MultiBinding.Bindings>
</MultiBinding>
</StackPanel.Visibility>
<RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
Content="<" />
<RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
Content="<=" />
<RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
Content="="
IsChecked="True" />
<RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
Content=">=" />
<RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=UserControl},
Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
Content=">" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</StackPanel>
</Border>
</Popup>
I have a very strange issue with it.
When I hit Tab, Left or Right focus leaves popup and goes to the parent window. Popus stays opened.
I do not know why this happens. But I've got broken keyboard navigation.
The only thing I can suspect to be the reason is that parent of Popup is UserControl hosted inside winforms ElementHost.
I have no idea how to debug this problem so I appreciate any helpful hints.
Thank you in advance.
Having a Binding on IsOpen can override the StaysOpen behavior. I can't tell without seeing more but if that IsOpen binding is persisting a True value from its source that would keep the Popup open no matter what happens with the focus.

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>

How do I make this trigger work?

I've created a UserControl for use in my app. It consists of a ComboBox in a Grid with two RepeatButtons. This control is for use in an application that will run on a touchscreen equipped laptop. The buttons are used to select the next or previous choice in the ComboBox. Here is the Xaml for the control:
<UserControl x:Class="CarSystem.CustomControls.TouchComboBox"
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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cs="clr-namespace:CarSystem.CustomControls"
mc:Ignorable="d"
Focusable="True"
GotFocus="UserControl_GotFocus">
<UserControl.Resources>
<Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource FocusedBackground}" />
<Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid Background="{Binding Path=GridBackground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
Width="{Binding Path=Width, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
DisplayMemberPath="{Binding Path=DisplayMemberPath, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
FlowDirection="{Binding Path=FlowDirection, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
Focusable="True"
FontFamily="{Binding Path=FontFamily, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
FontSize="{Binding Path=FontSize, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
FontStretch="{Binding Path=FontStretch, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
FontStyle="{Binding Path=FontStyle, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
FontWeight="{Binding Path=FontWeight, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
HorizontalAlignment="{Binding Path=HorizontalAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
HorizontalContentAlignment="{Binding Path=HorizontalContentAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
Grid.Column="0"
ItemsSource="{Binding Path=ItemsSource, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
Name="ChoicesPicker"
SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
SelectedValue="{Binding Path=SelectedValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
SelectedValuePath="{Binding Path=SelectedValuePath, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
SelectionChanged="ChoicesPicker_SelectionChanged"
Style="{StaticResource FocusedStyle}"
TabIndex="{Binding Path=TabIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
VerticalAlignment="Center"/>
<RepeatButton Background="{DynamicResource ButtonBackground}"
Click="SelectPreviousButton_Click"
Focusable="False"
Foreground="{DynamicResource ButtonForeground}"
Grid.Column="1"
IsTabStop="False"
Name="SelectPreviousButton">
<Image Source="/CustomControls;component/Resources/VolumeUp.png" />
</RepeatButton>
<RepeatButton Background="{DynamicResource ButtonBackground}"
Click="SelectNextButton_Click"
Focusable="False"
Foreground="{DynamicResource ButtonForeground}"
Grid.Column="2"
IsTabStop="False"
Name="SelectNextButton">
<Image Source="/CustomControls;component/Resources/VolumeDown.png" />
</RepeatButton>
</Grid>
</UserControl>
I want the Background and Foreground brushes to change when the ComboBox gets the focus. The Style named "FocusedStyle" in the UserControl Resources is supposed to do this for me, but it's not working. The Background and ForeGround colors never change.
What is wrong with my code?
Tony
You can't set a property from a style if that property is set in the actual control because any property set in the control takes precedence.
Try this:
<UserControl.Resources>
<Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource FocusedBackground}" />
<Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" />
</Trigger>
<Trigger Property="IsFocused" Value="False">
<Setter Property="Background" Value="{DynamicResource UnFocusedBackground}" />
<Setter Property="Foreground" Value="{DynamicResource UnFocusedForeground}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
Then you need to make a DynamicResource of the normal background.
Then remove the
Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
and the
Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
from the Combobox.
This way the trigger will be able to set the value of the property because the property isnt being set by the control itself.

Resources