Set style of a specific element of a UserControl by name - c#

If I have a UserControl with the following two Labels inside of it's grid like so:
<Grid x:Name="mainGrid">
<Label x:Name="labelTitle"/>
<Label x:Name="labelValue"/>
</Grid>
Can I set their styles separately from within a ResourceDictionary something like:
<Style TargetType="{x:Type MyControl}">
<Style.Resources>
<Style TargetType="MyControl.mainGrid.labelTitle">
</Style>
<Style TargetType="MyControl.mainGrid.labelValue">
</Style>
</Style.Resources>
</Style>
If possible I would like to do all of this in the ResourceDictionary and not have to touch the UserControl at all.

Try using a trigger in the style based on the name.
App.xaml
<Application x:Class="WpfApplication34.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication34"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type local:MyControl}">
<Style.Resources>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Style.Triggers>
<Trigger Property="Name" Value="labelTitle">
<Setter Property="Content" Value="This is the Title" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Trigger>
<Trigger Property="Name" Value="labelValue">
<Setter Property="Content" Value="This is the Value" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</Application.Resources>
</Application>
MyControl.xaml
<UserControl x:Class="WpfApplication34.MyControl"
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:local="clr-namespace:WpfApplication34"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="mainGrid">
<Label x:Name="labelTitle" />
<Label x:Name="labelValue" />
</Grid>
</UserControl>
MainWindow.xaml
<Window x:Class="WpfApplication34.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication34"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MyControl />
</Grid>
</Window>
Screenshot:

Related

How to override ComboBox ToggleButton from Material Design for XAML?

I'm new in WPF.
I have an issue with overriding MaterialDesignComboBoxToggleButton
style. I wanna to replace "Template" setter with own, but my content from control template always ignores. Why this occurred? With other styles i haven't this problem.
Bellow code demonstrates what i need.
Overriedes.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
App.xaml
<Application x:Class="Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-Wpf" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#FFD8E1FF" SecondaryColor="#FFD8E1FF" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="/Overrides.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Add the same resource key to your custom style like this:
<Style
x:Key="MaterialDesignComboBoxToggleButton"
BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}"
TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and you don't need to add resources like this in Overrides.xaml file:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
because the MDIX resources already included by MaterialDesignTheme.Defaults.xaml that you added in App.xaml file.
In my case override style in window not working ,but when I tried in App.xml then it works. I changed the style for toggle button by another style . the code is :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/IG/IG.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Themes/IG/IG.MSControls.Toolkit.Implicit.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border CornerRadius="8" BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF0F0F4B" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Background" Value="#FF73ADDE" />
<Setter Property="Foreground" Value="#FF150404" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Application.Resources>

WPF: Global resource accessible by key

I'm styling the Grid controls to be the table headers using resources like so:
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="5,10" />
<Setter Property="Foreground" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
<Style TargetType="Border">
<Setter Property="BorderThickness" Value="0.0,1.0,0.0,0" />
<Setter Property="BorderBrush" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
</Style>
</Grid.Resources>
The thing is, I need to apply that resources into multiple places in my app, which leads to the code being repeated.
I was wondering if this is possible to store the resources in my App.xaml and use them by the key or something like that? Like so:
<Resources Key="MyResourceSet">
<Style>
[..]
</Style>
</Resources>
<Grid Resource="MyResourceSet">
[...]
</Grid>
Place the Style in the App.Resources like you would in any other UIElement.
<Application x:Class="Question_Answer_WPF_App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Green" />
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="100" />
</Style>
</Application.Resources>
</Application>
Reference wherever you want in your app.
<Window x:Class="Question_Answer_WPF_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</Window>
Another way to do this if you want several ResourceDictionary's to be used across your app; but with the same inner keys, is to reference the unique ResourceDictionary per element that will use it. This will not be using the App.xaml resources but will be pointing directly to the file location in your application. Since ResourceDictionary's have a default 'Build Action' of 'Page' it will work referencing the location this way. If your ResourceDictionary doesn't work this way the first thing is to check this by right clicking the ResourceDictionary in your solution explorer and make sure that's correct.
Example:
MyCustomResourcesA.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="FontSize"
Value="46" />
</Style>
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Green" />
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="100" />
</Style>
</ResourceDictionary>
MyCustomResourcesB.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Question_Answer_WPF_App">
<Style TargetType="TextBlock">
<Setter Property="FontSize"
Value="26" />
</Style>
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Blue" />
<Setter Property="Height"
Value="20" />
<Setter Property="Width"
Value="200" />
</Style>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="Question_Answer_WPF_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<StackPanel HorizontalAlignment="Left">
<StackPanel>
<StackPanel.Resources>
<ResourceDictionary Source="MyCustomResourcesA.xaml" />
</StackPanel.Resources>
<TextBlock Text="I'm using MyCustomResourcesA" />
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</StackPanel>
<StackPanel>
<StackPanel.Resources>
<ResourceDictionary Source="MyCustomResourcesB.xaml" />
</StackPanel.Resources>
<TextBlock Text="I'm using MyCustomResourcesB" />
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</StackPanel>
</StackPanel>
</Window>
Looks like:

Oxyplot - Hiding Tick Mark Labels

I am trying to hide the first and last tick mark label on the x-axis. I've done it once via a style and style triggers, but that code has gone....somewhere. Here's what I'm working with.
<UserControl
x:Class="PentagearRT.Controls.Graph"
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:oxy="http://oxyplot.org/wpf"
xmlns:local="clr-namespace:PentagearRT.Controls"
mc:Ignorable="d"
d:DesignHeight="500"
d:DesignWidth="800">
<Grid>
<oxy:Plot>
<oxy:Plot.Resources>
</oxy:Plot.Resources>
<oxy:Plot.Axes>
<oxy:LinearAxis Position="Left" Minimum="-5" Maximum="5" MajorStep="1"
MajorGridlineColor="AliceBlue" MajorGridlineThickness=".07" MajorGridlineStyle="Dot"/>
<oxy:LinearAxis Position="Bottom" Minimum="0" Maximum="360" MajorStep="45" StringFormat="0°"
MajorGridlineColor="AliceBlue" MajorGridlineThickness=".07" MajorGridlineStyle="Dot"/>
</oxy:Plot.Axes>
<oxy:LineSeries Background="Black"/>
</oxy:Plot>
</Grid>
</UserControl>
Edit:
What I'm trying to hide are the two values circled in red.
<oxy:Plot.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="360°">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="Text" Value="0°">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
</oxy:Plot.Resources>

CalendarStyle of DatePicker different than my custom CalendarStyle

I have a custom Calendar style and a custom DatePicker style.
Code in my MyCustomSkin.xaml (from my library project)
<Style TargetType="{x:Type Calendar}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="Background" Value="Green"/>
</Style>
They are both inside my MyCustomSkin.xaml ResourceDictionary (separate solution that generate a MyCustomSkin.dll) that I reference inside my application.
Code in my App.xaml (from my application)
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyCustomSkin;component/MyCustomSkin.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Code in my MainWindow.xaml (from my application)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<DatePicker HorizontalAlignment="Right" />
<Calendar HorizontalAlignment="Left"/>
</Grid>
</Window>
When I create a Calendar in my application (without specifying any style), the background is red.
When I am creating a DatePicker in my application (without specifying any style), the background is green but the background of its calendar is the system one (=> not red).
Why? I thougt that I was overriding all the system calendars with mine. Why is it applying the custom style for the Calendar but not on the Calendar of DatePicker?
Thank you!
Your MyCustomSkin.xaml should be:
<Style TargetType="{x:Type Calendar}" >
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type DatePicker}" >
<Setter Property="Background" Value="Green" />
<Setter Property="CalendarStyle">
<Setter.Value>
<Style TargetType="{x:Type Calendar}" BasedOn="{StaticResource {x:Type Calendar}}"/>
</Setter.Value>
</Setter>
</Style>
DatePicker has a default CalendarStyle which will be applied on component initialization.

Creating styles for a ListView in a WPF Desktop application

I have some styles for a Metro/win8 app:
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
But now I am making a desktop app in wpf (.net 4.5) and cannot apply styles like this in xaml to a ListView control. How do we define our own custom styles for a Desktop ListView control in xaml?
Here is an example putting the style in a windows resource dictionary.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>
and if you want the style to be placed in it's own file then you can reference that file like this (my resource file is just call Dictionary1.xaml)
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>

Categories

Resources