How to replace Maximize/RestoreDown icons of MetroWindow of MahApps - wpf

I am using MahApps.Metro in my WPF app and trying to change the Maximize/RestoreDown icons of MetrowWindow to the following new FullScreen/BackToWindow icons:
Question: How can we replace the default Maximize/RestoreDown icons of MetrowWindow?
Remarks: As suggested here, I tried the following code but it's still showing the old style - as shown below. I'm probably missing something here and not doing it right. New icons are not an issue. I just need to know how we can replace the old ones with the new ones.
<mah:MetroWindow x:Class="WPF_Mah_Metro_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Mah_Metro_Test"
mc:Ignorable="d"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
Height="450" Width="800">
<mah:MetroWindow.WindowButtonCommands>
<mah:WindowButtonCommands Template="{DynamicResource MahApps.Templates.WindowButtonCommands.Win10}" />
</mah:MetroWindow.WindowButtonCommands>
<Grid>
</Grid>

In order to customize the window buttons, you have to create a custom control template for the type WindowButtonCommands. You can copy one of the default control templates below from GitHub.
MahApps.Templates.WindowButtonCommands
MahApps.Templates.WindowButtonCommands.Win10
I took the Win10 control template and and adapted two things in the PART_Max button:
Change the Data of PART_MaxPath Path to an appropriate path that represents the icon or replace it with a TexBlock that uses the Segoe MDL2 Assets font and displays the gylph.
Change the DataTrigger for Maximized to set the appropriate icon the same way.
In the example below, I used the TextBlock approach, because I do not have path data for the icons. Please be aware, that the Path variant would always work, while the TextBlock variant requires the font to be available on your system.
<ControlTemplate x:Key="MahApps.Templates.WindowButtonCommands.Custom"
TargetType="{x:Type mah:WindowButtonCommands}">
<StackPanel Orientation="Horizontal">
<Button x:Name="PART_Min"
Width="46"
AutomationProperties.AutomationId="Minimize"
AutomationProperties.Name="Minimize"
Command="{x:Static SystemCommands.MinimizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMinButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}}"
ToolTip="{Binding Minimize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}"
ConverterParameter="MIN">
<Binding Mode="OneWay"
Path="ShowMinButton"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Path Width="10"
Height="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0L10,0 10,1 10,1 1,1 0,1z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Stretch="Uniform" />
</Button>
<Button x:Name="PART_Max"
Width="46"
AutomationProperties.AutomationId="MaximizeRestore"
AutomationProperties.Name="Maximize"
Command="{x:Static SystemCommands.MaximizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMaxRestoreButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}}"
ToolTip="{Binding Maximize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}"
ConverterParameter="MAX">
<Binding Mode="OneWay"
Path="ShowMaxRestoreButton"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<!-- normal state -->
<TextBlock x:Name="PART_MaxPath"
FontFamily="Segoe MDL2 Assets"
FontSize="16"
Text="" />
</Button>
<Button x:Name="PART_Close"
Width="46"
AutomationProperties.AutomationId="Close"
AutomationProperties.Name="Close"
Command="{x:Static SystemCommands.CloseWindowCommand}"
Focusable="False"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=IsCloseButtonEnabled, Mode=OneWay}"
ToolTip="{Binding Close, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}"
ConverterParameter="CLOSE">
<Binding Mode="OneWay"
Path="ShowCloseButton"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type mah:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Path Width="10"
Height="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="F1M8.583,8L13,12.424 12.424,13 8,8.583 3.576,13 3,12.424 7.417,8 3,3.576 3.576,3 8,7.417 12.424,3 13,3.576z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Stretch="Uniform" />
</Button>
</StackPanel>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=IsCloseButtonEnabled}"
Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=IsAnyDialogOpen}"
Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=IsCloseButtonEnabledWithDialog}"
Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="PART_Close"
Property="IsEnabled"
Value="False" />
</MultiDataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type mah:MetroWindow}}, Path=WindowState}"
Value="Maximized">
<Setter TargetName="PART_Max"
Property="AutomationProperties.Name"
Value="Restore" />
<Setter TargetName="PART_Max"
Property="Command"
Value="{x:Static SystemCommands.RestoreWindowCommand}" />
<Setter TargetName="PART_Max"
Property="ToolTip"
Value="{Binding Restore, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
<Setter TargetName="PART_MaxPath"
Property="Text"
Value="" />
</DataTrigger>
<Trigger Property="Theme"
Value="Light">
<Setter TargetName="PART_Close"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightCloseButtonStyle}" />
<Setter TargetName="PART_Max"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMaxButtonStyle}" />
<Setter TargetName="PART_Min"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMinButtonStyle}" />
</Trigger>
<Trigger Property="Theme"
Value="Dark">
<Setter TargetName="PART_Close"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkCloseButtonStyle}" />
<Setter TargetName="PART_Max"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMaxButtonStyle}" />
<Setter TargetName="PART_Min"
Property="Style"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMinButtonStyle}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Set the custom WindowButtonCommands template MahApps.Templates.WindowButtonCommands.Custom to MetroWindow the same way as in your question. Make sure the control template resource is in scope.
<mah:MetroWindow.WindowButtonCommands>
<mah:WindowButtonCommands Template="{StaticResource MahApps.Templates.WindowButtonCommands.Custom}" />
</mah:MetroWindow.WindowButtonCommands>

Related

Control in Display Template loosing value after Edit template is put to action in dxg:GridControl's GridColumn

i have a dxg:GridControl in which i have dxg:GridColumn and on every row there is a Display and Edit template like below
<dxg:GridControl>
<dxg:GridControl.Columns>
<dxg:GridColumn Width="101"
AllowBestFit="True"
AllowColumnFiltering="True"
AllowSorting="False"
FieldName="Tolerance"
Header="Tolerance Type"
ReadOnly="{Binding IsReadOnly}">
<dxg:GridColumn.EditTemplate>
<ControlTemplate>
<dxe:ComboBoxEdit
x:Name="PART_Editor"
Width="100"
ItemsSource="{Binding Path=DataContext.ToleranceTypeCollection,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding RowData.Row.Tolerance}"
DisplayMember="ToleranceType"
FilterCondition="Contains"
ImmediatePopup="True"
IncrementalFiltering="True"
IsTextEditable="False"
ValueMember="ToleranceTypeId"
ShowSizeGrip="True"
TextWrapping="WrapWithOverflow"
Style="{DynamicResource DXEComboBoxEditStyle}" />
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.ToleranceTypeCollection.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="0">
<Setter TargetName="PART_Editor" Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding DataContext.IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="True">
<Setter TargetName="PART_Editor" Property="IsEnabled" Value="False" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</dxg:GridColumn.EditTemplate>
<dxg:GridColumn.DisplayTemplate>
<ControlTemplate>
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center" Text="{Binding RowData.Row.Tolerance.ToleranceType}" />
</ControlTemplate>
</dxg:GridColumn.DisplayTemplate>
</dxg:GridColumn>
</dxg:GridControl.Columns>
</dxg:GridControl>
the values are coming well on the Display template but while clicking on Cell, the control is changing to dxe:ComboBoxEdit but it is not automatically picking the saved value from List of values in drop down hence the value on Display templates's textbox is being lost after clicking somewhere else.
Using Dev Express v 18.1
Please help on the same
Thank you

Button visibility depending on two properties (OR not AND)

I have a button in WPF and I want to make it visible if the mouse is over the border the button is in. This is simple:
<Button Grid.Column="1" Click="DimFilter_Click" Style="{StaticResource ImageButton}" MouseDown="Button_MouseDown"
Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver, Converter={StaticResource BoolToVis}}">
<Button.Content>
<Image Source="{Binding FilterActive, Converter={StaticResource FilterImageConverter}}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button.Content>
</Button>
My problem is that I also want to make button visible if the Property "FilterActive" is true.
So if "FilterActive" is false I want to use the MouseOver Binding in the Code above, but if "FilterActive" is true, the Button should be visible all the time, ignoring the first binding.
Is this possible in any way?
You could use triggers rather than a binding:
<Button Grid.Column="1"
Click="DimFilter_Click"
MouseDown="Button_MouseDown">
<Button.Style>
<Style BasedOn="{StaticResource ImageButton}" TargetType="Button">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding FilterActive}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver, Converter={StaticResource BoolToVis}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.Content>
<Image Source="{Binding FilterActive, Converter={StaticResource FilterImageConverter}}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button.Content>
</Button>
Okay I got it, stupid me.
<Button.Visibility>
<MultiBinding Converter="{StaticResource ButtonVisConverter}">
<Binding Path="FilterActive"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Border}}" Path="IsMouseOver"/>
</MultiBinding>
</Button.Visibility>
The code in the converter is pretty much self explanatory

Command Binding Failure in MenuItem

I have the following MenuItem template in a resource dictionary
<Style x:Key="RecentMenuItem"
TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource
Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.LoadRecentItemCommand}" />
<Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Header}"/>
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}. {1}">
<Binding Path="(ItemsControl.AlternationIndex)"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}"/>
<Binding Path="FullFileName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
This is used like
<Menu>
<MenuItem Header="FILE">
...
<MenuItem Header="_Recent Studies"
ItemsSource="{Binding RecentFiles}"
AlternationCount="{Binding Path=Items.Count,
Mode=OneWay,
RelativeSource={RelativeSource Self}}"
ItemContainerStyle="{StaticResource RecentMenuItem}"/>
<MenuItem/>
The binding on the Command is not working (I can see this with Snoop[dog]).
What is wrong with the above command binding and how can I fix it?
Thanks for your time.
I tried this and it worked just fine... i got the full file name in my command parameter: Here command is defined in my window's VM, so you will have to update it accordingly (if you used usercontrol).
<Menu>
<MenuItem Header="_Recent Studies"
ItemsSource="{Binding Files}"
ItemContainerStyle="{StaticResource RecentMenuItem}"/>
</Menu>
<Style x:Key="RecentMenuItem"
TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command" Value="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
<Setter Property="CommandParameter" Value="{Binding FullName}"/>
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<Binding Path="FullName"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

DataBinding to RelativeSource Tag

I am trying to use the Tag property as the source of a Binding, but the value is null when it gets to the converter.
What am I doing wrong?
Consumer
<Button Style="{StaticResource AddNewItemButtonStyle}" Tag="blah" />
Binding
<Style x:Key="AddNewItemButtonStyle" BasedOn="{StaticResource blueButtonStyle}"
TargetType="{x:Type Button}">
...
<AccessText Text="{Binding RelativeSource={RelativeSource Self},
Path=Tag, Converter={StaticResource AddNewItemForLabel}}">
</Style>
UPDATE
I added a setter for the ToolTip using the same strategy, and that does work BUT only after the second call to the converter (triggered by mousing over).
Can you see why the binding wouldn't work on the first pass?
Is there some place else besides the Tag that I can use more reliably?
2nd UPDATE
Based on Phil's input I changed my style to the xaml below. Must I add a Template property to the style?
<Style x:Key="AddNewItemButtonStyle" BasedOn="{StaticResource blueButtonStyle}" TargetType="{x:Type Button}">
<Setter Property="resx:ResxExtension.DefaultResxName" Value="Smack.Core.Presentation.Resources.MasterDetail"/>
<Setter Property="Content" >
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Image Source="{resx:Resx ResxName=Smack.Core.Presentation.Resources.MasterDetail, Key=bullet_add}" Stretch="Uniform" />
<AccessText VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Converter={StaticResource AddNewItemForLabel}}" />
<ContentPresenter/>
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Converter={StaticResource AddNewItemForToolTip}}"/>
<Setter Property="Command" Value="{Binding AddNewItemCommand}" />
</Style>
If you change the xaml in the answer I gave to your other question to
<AccessText Grid.Column="1" VerticalAlignment="Center">
<AccessText.Text>
<MultiBinding StringFormat="{}_{0} {1}">
<Binding Source="{StaticResource Test}"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Tag"/>
</MultiBinding>
</AccessText.Text>
</AccessText>
Then Tag will work.
Or you can use the short form of TemplateBinding
<AccessText Grid.Column="1" VerticalAlignment="Center" Text="{TemplateBinding Tag}"/>
or the long form
<AccessText Grid.Column="1" VerticalAlignment="Center"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag}"/>
or, your style will work like this (bits deleted for testing):
<Style x:Key="AddNewItemButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Content" >
<Setter.Value>
<StackPanel Orientation="Horizontal">
<AccessText VerticalAlignment="Center"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Tag}" />
<ContentPresenter/>
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}"/>
</Style>

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