XAML Warning "The resource could not be resolved" - wpf

I am using DynamicResources in WPF, I have made a button style in app.xaml. Below is the definition:
<Application.Resources>
<Style x:Key="InteractionButtonStyle"
TargetType="{x:Type Button}">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
<SolidColorBrush x:Key="StyleBasedForegroundColor" Color="White"/>
<SolidColorBrush x:Key="StyleBasedBackgroundColor" Color="Black" />
</Style.Resources>
<Setter Property="Background" Value="{StaticResource StyleBasedBackgroundColor}" />
<Setter Property="Foreground" Value="{StaticResource StyleBasedForegroundColor}" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="10" />
</Style>
</Application.Resources>
Now when I use this style in a page:
<Button Grid.Row="3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Click="RegisterNew_Clicked"
Style="{StaticResource InteractionButtonStyle}"
Visibility="{Binding ShowRegisterVeteranOption, Converter={StaticResource BoolToVisibilityConverter}}">
<StackPanel Orientation="Horizontal">
<Viewbox Height="20"
Width="20">
<Path Data="M9.6110058,5.399L11.650064,5.399 11.650064,9.6789093 15.929999,9.6789093 15.929999,11.717999 11.650064,11.717999 11.650064,15.998 9.6110058,15.998 9.6110058,11.717999 5.3309995,11.717999 5.3309995,9.6789093 9.6110058,9.6789093z M10.66645,1.6420071C5.6808301,1.6420071 1.6404767,5.6823504 1.6404767,10.66655 1.6404767,15.650881 5.6808301,19.691223 10.66645,19.691223 15.64948,19.691223 19.689723,15.650881 19.689723,10.66655 19.689723,5.6823504 15.64948,1.6420071 10.66645,1.6420071z M10.66645,0C16.546567,0 21.333001,4.785153 21.333001,10.66655 21.333001,16.548068 16.546567,21.333 10.66645,21.333 4.7836227,21.333 0,16.548068 0,10.66655 0,4.785153 4.7836227,0 10.66645,0z"
Fill="{DynamicResource StyleBasedForegroundColor}" />
</Viewbox>
<TextBlock Text="Register"
Margin="10,0,0,0"
Foreground="{DynamicResource StyleBasedForegroundColor}"
VerticalAlignment="Center" />
</StackPanel>
</Button>
Although, things work when the app is running, I get this annoying warning:
The resource "StyleBasedForegroundColor" could not be resolved.
I looked up other questions, but could not find a way that doesn't involve themes.xaml or ResourceDictionary

Related

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>

WPF Styling: BasedOn template but change 2 difference background colours

I have got a button with a ControlTemplate, this ControlTemplate has 2 different Control's with different background colours. I want to be able to change these colours using a new style which is BasedOn this main style.
Read the code and it should make more sense. For some reason though the new Style.Resources are not changing the background colours, they are still using the two defined in the main style.
How can I fix this?
Style BasedOn="{StaticResource ButtonIcoSmall}" TargetType="Button" x:Key="greenSmallButton">
<Style.Resources>
<SolidColorBrush Color="#FF8AB71C" x:Key="colour1" />
<SolidColorBrush Color="#FF72A000" x:Key="colour2" />
</Style.Resources>
</Style>
<Style BasedOn="{StaticResource ButtonIcoSmall}" TargetType="Button" x:Key="greySmallButton">
<Style.Resources>
<SolidColorBrush Color="#FF434953" x:Key="colour1" />
<SolidColorBrush Color="#FF22252b" x:Key="colour2" />
</Style.Resources>
</Style>
<Style x:Key="ButtonIcoSmall" TargetType="Button">
<Style.Resources>
<SolidColorBrush Color="#FF434953" x:Key="colour1" />
<SolidColorBrush Color="#FF22252b" x:Key="colour2" />
</Style.Resources>
<Setter Property="FontFamily" Value="{StaticResource FontAwesome}" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="8" />
<Setter Property="Margin" Value="6" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<WrapPanel>
<Border Grid.Column="0"
CornerRadius="5 0 0 5"
BorderThickness="0"
Background="{StaticResource colour1}"
Name="buttonIcon">
<WrapPanel>
<ContentPresenter Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="{TemplateBinding Margin}" />
</WrapPanel>
</Border>
<Border Grid.Column="0"
CornerRadius="0 5 5 0"
BorderThickness="0"
Background="{StaticResource colour2}"
Name="buttonText" >
<TextBlock Text="{TemplateBinding Tag}"
FontSize="13"
Foreground="White"
Padding="{TemplateBinding Padding}" />
</Border>
</WrapPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding ElementName=buttonIcon}" TargetName="buttonText" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter Property="Background" Value="#757b8d" />
<Setter Property="Foreground" Value="White" />-->
</Trigger>
</Style.Triggers>
</Style>
Two things:
You should move you main style above the derived styles. If you use BasedOn before the style that its basing itself on, it will not find it.
For the colors that you are changing, use a DynamicResource instead. This allows the two colors to be re-defined instead of using the defaults.
I got it working using the following code:
<Window x:Class="WpfApplication1.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"
mc:Ignorable="d"
x:Name="TestWindow">
<Window.Resources>
<Style x:Key="ButtonIcoSmall" TargetType="Button">
<Style.Resources>
<SolidColorBrush Color="#FF434953" x:Key="colour1" />
<SolidColorBrush Color="#FF22252b" x:Key="colour2" />
</Style.Resources>
<Setter Property="FontFamily" Value="{StaticResource FontAwesome}" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="8" />
<Setter Property="Margin" Value="6" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<WrapPanel>
<Border Grid.Column="0"
CornerRadius="5 0 0 5"
BorderThickness="0"
Background="{DynamicResource colour1}"
Name="buttonIcon">
<WrapPanel>
<ContentPresenter Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="{TemplateBinding Margin}" />
</WrapPanel>
</Border>
<Border Grid.Column="0"
CornerRadius="0 5 5 0"
BorderThickness="0"
Background="{DynamicResource colour2}"
Name="buttonText" >
<TextBlock Text="{TemplateBinding Tag}"
FontSize="13"
Foreground="White"
Padding="{TemplateBinding Padding}" />
</Border>
</WrapPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding ElementName=buttonIcon}" TargetName="buttonText" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter Property="Background" Value="#757b8d" />
<Setter Property="Foreground" Value="White" />-->
</Trigger>
</Style.Triggers>
</Style>
<Style BasedOn="{StaticResource ButtonIcoSmall}" TargetType="Button" x:Key="greenSmallButton">
<Style.Resources>
<SolidColorBrush Color="#FF8AB71C" x:Key="colour1" />
<SolidColorBrush Color="#FF72A000" x:Key="colour2" />
</Style.Resources>
</Style>
<Style BasedOn="{StaticResource ButtonIcoSmall}" TargetType="Button" x:Key="greySmallButton">
<Style.Resources>
<SolidColorBrush Color="#FF434953" x:Key="colour1" />
<SolidColorBrush Color="#FF22252b" x:Key="colour2" />
</Style.Resources>
</Style>
</Window.Resources>
<Button Style="{StaticResource greenSmallButton}"/>
</Window>

Scroll WPF toolkit chart in touch screen

I am using a WPF toolkit chart in my application which is for touch screen devices. I want to enable scrolling for the chart but don't want to show scroll bar to the user. How can I achieve this? Here is my code:-
<ScrollViewer>
<chartingToolkit:Chart Name="bottomChart" BorderThickness="0" Padding="0" ClipToBounds="False" >
<chartingToolkit:Chart.PlotAreaStyle>
<Style TargetType="Grid">
<Setter Property="Background" Value="Transparent" />
</Style>
</chartingToolkit:Chart.PlotAreaStyle>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="False" Visibility="Hidden" Width="0" Minimum="0"/>
<chartingToolkit:LinearAxis Orientation="X" Interval="5" ShowGridLines="True" Visibility="Hidden" Height="0" >
</chartingToolkit:LinearAxis>
</chartingToolkit:Chart.Axes>
<chartingToolkit:Chart.TitleStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
<Setter Property="Visibility" Value="Hidden" />
</Style>
</chartingToolkit:Chart.TitleStyle>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
<Setter Property="Visibility" Value="Hidden" />
</Style>
</chartingToolkit:Chart.LegendStyle>
<TestManagementUC:UnorderedLineSeries x:Name="BottomChartSeries" DependentValueBinding="{Binding Path=Value}" IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding Path=DataPoints}">
<TestManagementUC:UnorderedLineSeries.DataPointStyle>
<Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
<Setter Property="Background" Value="{StaticResource Brush}" />
<Setter Property="Template" Value="{x:Null}" />
</Style>
</TestManagementUC:UnorderedLineSeries.DataPointStyle>
</TestManagementUC:UnorderedLineSeries>
</chartingToolkit:Chart>
</ScrollViewer>

Add control in resource dictionary style

I'm not sure if it's even possible, but worth to try and ask. So - it is possible to add somehow control to resource dictionary?
For example I have style which I apply to every window in my application:
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="WindowState" Value="Maximized" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidth}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeight}}" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/myApp;component/Pictures/gradientBackground.png" />
</Setter.Value>
</Setter>
<Setter Property="Icon" Value="/myApp;component/Pictures/logo_small.png" />
</Style>
As you can see every window is maximized and have no borders etc - it's just full screen window. What I'd like to do is add simple button in the right-top corner, which will work as minimize.
Any idea?
Thanks in advance!
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="WindowState" Value="Maximized" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidth}}" />
<Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeight}}" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/myApp;component/Pictures/gradientBackground.png" />
</Setter.Value>
</Setter>
<Setter Property="Icon" Value="/myApp;component/Pictures/logo_small.png" />
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Content="X" HorizontalAlignment="Right" Margin="2"></Button>
<ContentControl Grid.Row="1" Content="{TemplateBinding Content}"></ContentControl>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

How to wrap Text in a ListViewItem?

WPF ListBoxItem how to wrap text in it? My Item container style looks like this:
<Style x:Key="GroupListBoxItemStyle"
TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="FocusVisualStyle"
Value="{x:Null}" />
<Setter Property="FontSize"
Value="11" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Width"
Value="95" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<SlidingBar:SlidingBarRadioButton GroupName="PermissionsRadioButtonGroup"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},BindsDirectlyToSource=True,Mode=TwoWay}"
Text="{Binding Converter={StaticResource resourceStringToResourceConverter}}"
ImageSource="{Binding Converter={StaticResource PermissionTypeToImageConverter}}"
Margin="1"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Make sure you set the following properties to the ListBox/ListView:
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Resources