Multiple Style Resource in WPF Metro Button - wpf

I am trying to achieve my save button to reusable button and original button style is like this=>
<Button Margin="5"
Padding="0"
Width="98"
Cursor="Hand"
x:Name="btnSave"
Click="btnSave_Click">
<StackPanel Orientation="Horizontal" Height="25" Width="90">
<Image Source="\Image\Other\Save.ico" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
</Button>
Just text and image. So I just want to use this button as a reusable button. So I move this button to App.xaml Like this=>
<Style TargetType="Button" x:Key="SaveButton" BasedOn="{StaticResource MetroButton}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Style.Resources>
<Style TargetType="StackPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="90"/>
<Setter Property="Background" Value="Red"/>
<Style.Resources>
<Style TargetType="Image">
<Setter Property="Source" Value="/Image/Other/Save.ico"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Margin" Value="3 0"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="15 0"/>
<Setter Property="Text" Value="Save"/>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
But after moving that, This button is not working anymore. Please let me know why this one is not working.

You said you wanted to reuse styles, so you shouldn't nest them. The right thing to do is:
<Style x:Key="SaveButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal" Height="25" Width="90" Background="Red">
<Image/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

You need to define the StackPanel somewhere and set the Button's Content property to it. You may define the StackPanel as a non-shared resource next to the Style in App.xaml like this:
<StackPanel x:Key="sp" x:Shared="False" Orientation="Horizontal" Height="25" Width="90">
<Image Source="screen.png" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
<Style TargetType="Button" x:Key="SaveButton">
<Style.Resources>
</Style.Resources>
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Content" Value="{StaticResource sp}" />
</Style>

Related

WPF Style Foreground of RadioButton not applied

I have a style for my textblocks, and a style for my radio button.
When I add my radio buttons, they ignore the style for my foreground. The textblock style seems to have precedence and I can't get it the way I want it.
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="FontFamily" Value="Daytona Light"/>
<Setter Property="FontSize" Value="22"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Foreground" Value="White"/>
</Style>
<Style x:Key="Radio" TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Foreground" Value="#1a3281"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Border
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#1a3281" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I create my radio buttons in a stack panel
<StackPanel Grid.Column="1" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="RadioButton" BasedOn="{StaticResource Radio}">
<Setter Property="Margin" Value="50, 0, 0, 20"/>
<Setter Property="Width" Value="250"/>
<Setter Property="Height" Value="60"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</StackPanel.Resources>
<RadioButton GroupName="MainSelection" Command="{Binding ShowVideoCommand}" Content="Video">
</RadioButton>
<RadioButton GroupName="MainSelection" Command="{Binding ShowPresentationCommand}" Content="Presentation">
</RadioButton>
<RadioButton GroupName="MainSelection" Command="{Binding ShowSimulatorCommand}" Content="Simulator">
</RadioButton>
</StackPanel>
When I run my app, I see the radio buttons as full white rectangles. When the RadioButton is checked, the style for the background works fine (becomes blue, with text still white).
What am I doing wrong for my foreground style? Why is the textblock style always wins over my radio button style?
Even when I set Foreground="Red" in my RadioButton as follows
<RadioButton Foreground="Red" GroupName="MainSelection" Command="{Binding ShowVideoCommand}" Content="Video">
it won't show up. Text is always white.

WPF align menu items

I want to align menu items to vertical center. But VerticalAlignment doesn't work and finally it's looks like
<Menu Background="{x:Null}" Style="{StaticResource m_menu_Menu}">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="PlusCircle" Style="{StaticResource m_top_menu_materialDesign_Farm}" Height="43" Width="45"/>
<MenuItem x:Name="m_addWork" Header="{DynamicResource loc_menu_top_Add}" />
<materialDesign:PackIcon Kind="ChevronDown" Style="{StaticResource m_top_menu_materialDesign_Farm}"/>
</StackPanel>
</Menu>
I want to cool cell selection like, but align and header item selection look awful. How can I fix problem with vertical alignment?
After some changes i have:
<Style TargetType="Menu" x:Key="m_menu_Menu">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="FontFamily" Value="Roboto"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
<Style TargetType="materialDesign:PackIcon" x:Key="m_top_menu_materialDesign_Farm">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
<Menu Background="{x:Null}" Style="{StaticResource m_menu_Menu}" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="PlusCircle" Style="{StaticResource m_top_menu_materialDesign_Farm}" Height="43" Width="45"/>
<MenuItem x:Name="m_addWork" Header="Add" />
<materialDesign:PackIcon Kind="ChevronDown" Style="{StaticResource m_top_menu_materialDesign_Farm}"/>
</StackPanel>
</Menu>
But it's looks ugly yet:
I solve my main issue with MenuItem.Header and inner StackPanel. Code:
<Style TargetType="Menu" x:Key="m_menu_Menu">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="FontFamily" Value="Roboto"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
<Style TargetType="TextBlock" x:Key="m_top_menu_TextBlock_Farm">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="FontFamily" Value="Roboto"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Margin" Value="0 0 10 0"></Setter>
</Style>
<Style TargetType="materialDesign:PackIcon" x:Key="m_top_menu_materialDesign_Farm">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
<Menu Background="{x:Null}" Style="{StaticResource m_menu_Menu}" VerticalAlignment="Stretch">
<MenuItem x:Name="m_addWork" Height="75">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="PlusCircle" Style="{StaticResource m_top_menu_materialDesign_Farm}" Height="43" Width="45"/>
<TextBlock Text="Add" Style="{StaticResource m_top_menu_TextBlock_Farm}"/>
<materialDesign:PackIcon Kind="ChevronDown" Style="{StaticResource m_top_menu_materialDesign_Farm}"/>
</StackPanel>
</MenuItem.Header>
</MenuItem>
</Menu>

Set button hover or onmouseover background color

I am struggling to find a solution to this. Every other one i;ve tried is not working. Maybe I am missing something.
I want to change the background color of a Button when I hover over it with the mouse.
This is my XAML:
<Window x:Class="Recruitment_Manager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="374" Width="940">
<Window.Resources>
<Style TargetType="Button" x:Key="NavigationButtonStyle">
<Setter Property="Height" Value="35"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="StackPanel" x:Key="NavigationButtonContentStyle">
<Setter Property="Orientation" Value="Horizontal"/>
</Style>
<Style TargetType="Image" x:Key="NavigationButtonImageStyle">
<Setter Property="Width" Value="35"/>
</Style>
<Style TargetType="Label" x:Key="NavigationButtonLabelStyle">
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</Window.Resources>
<Grid>
<StackPanel HorizontalAlignment="Left" Width="200">
<StackPanel.Background>
<SolidColorBrush Color="#404040"/>
</StackPanel.Background>
<Button Style="{StaticResource ResourceKey=NavigationButtonStyle}">
<StackPanel Style="{StaticResource ResourceKey=NavigationButtonContentStyle}">
<Image Style="{StaticResource ResourceKey=NavigationButtonImageStyle}">
</Image>
<Label Style="{StaticResource ResourceKey=NavigationButtonLabelStyle}">
Settings
</Label>
</StackPanel>
</Button>
</StackPanel>
</Grid>
But it looks like the trigger never executes?
What am I missing or how can I get what I want?
Thank you in advance.
To remove the default MouseOver behaviour on the Button you will need to modify the ControlTemplate. Changing your Style definition to the following should do the trick:
<Style TargetType="{x:Type Button}" x:Key="NavigationButtonStyle">
<Setter Property="Height" Value="35"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
Try following code
<Style TargetType="Button" x:Key="NavigationButtonStyle">
...
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style>

Change properties of controls from inherited styles in wpf xaml

I have these styles defined:
<Style x:Key="ImgButton" TargetType="Button">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ImgButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="ButtonImage" Stretch="Uniform" Height="20" Width="20" Source="">
</Image>
<TextBlock Name="ButtonText" Margin="4,0" VerticalAlignment="Center" Text=""></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
I want to create another style, LoginButtonWithText (shown below), that is based on ImgButtonWithText, and which sets the value of the Source property of the ButtonImage control in the ImgButtonWithText style. I want to do the same with the Text property of the ButtonText textblock.
<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButtonWithText}">
<Setter Property="ToolTip" Value="Login"></Setter>
<Setter Property="IsDefault" Value="True"></Setter>
//How do I set the Image Source value and TextBlock Text value here?
</Style>
I found this question, but it doesn't appear to be quite exactly what I'm looking for. How do I change the value of child controls from an inherited style, or am I doing it all wrong?
It seems that what I want to do is not exactly possible, but I've done the next best thing in defining an image button style to use in all styles that inherit from from ImgButtonWithText:
<Style x:Key="ImgButton" TargetType="Button">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ButtonImage" TargetType="Image">
<Setter Property="Height" Value="20"></Setter>
<Setter Property="Width" Value="20"></Setter>
<Setter Property="Stretch" Value="Uniform"></Setter>
</Style>
<Style x:Key="ButtonText" TargetType="TextBlock">
<Setter Property="Margin" Value="4,0"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="ButtonImage" Style="{StaticResource ButtonImage}" Source="../Images/login.png">
</Image>
<TextBlock Name="ButtonText" Style="{StaticResource ButtonText}" Text="Let's Go"></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

Why does not Style.BasedOn work for the style of a child control if parent style was defined in Style.Resources of a parent?

I have the style:
<Style TargetType="Panel" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type styles:CommonStyles}, ResourceId=ButtonsPanelStyle}">
<Style.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="MinWidth" Value="75"/>
</Style>
</Style.Resources>
<Setter Property="Background" Value="#E0E0E0"/>
<Setter Property="StackPanel.Orientation" Value="Horizontal"/>
<Setter Property="FlowDirection" Value="RightToLeft"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="MinWidth" Value="{Binding RelativeSource={RelativeSource Self}, Path=DesiredSize.Width}"/>
</Style>
And its usage:
<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
Style="{DynamicResource {x:Static styles:CommonStyles.ButtonsPanelStyleKey}}">
<Button Content="Add" Command="{Binding Add}" Grid.RowSpan="2" Height="23" HorizontalAlignment="Right" Name="AddButton" VerticalAlignment="Center" TabIndex="2" >
<Button.Style>
<Style BasedOn="{StaticResource ResourceKey={x:Type Button}}" TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ActionType}" Value="Edit">
<Setter Property="Button.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DockPanel>
The style applied to button does not respect margin set in the style defined inside style's resources. Why?

Resources