I have defined a custom Windows control template inside Libraries\WindowsTemplates:
<ControlTemplate x:Key="DefaultWindowTemplate" TargetType="{x:Type Window}">
<Border BorderBrush="Red" BorderThickness="2">
<Grid Margin="3 3 3 3 " Width="auto" Height="auto">
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<UniformGrid Rows="1" Columns="1" Height="75" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0" >
<Rectangle Stretch="Fill" Fill="{StaticResource SteelBrush_Vert}" Stroke="Black" RenderTransformOrigin="0.5,0.5"/>
</UniformGrid>
</Grid>
</Border>
</ControlTemplate>
I have referenced the ControlTemplate in App.xaml:
<ResourceDictionary x:Key="WindowTemplates">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I try to use the template in my window:
Window x:Class="QCast.Windows.RawMatEntry"
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:QCast.Windows"
mc:Ignorable="d"
Title="RawMatEntry" Height="450" Width="800"
Template="{DynamicResource DefaultWindowTemplate}">
And still I get the error that the resource cannot be resolved. What am I missing?
My entire app.xaml looks like this:
<Application x:Class="QCast.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:QCast"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.FontSize" Value="14"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ResourceDictionary x:Key="MyDictionaries">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SteelBrush.xaml"/>
<ResourceDictionary Source="FullSteel.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<ResourceDictionary x:Key="LEDs">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MindFusion.Gauges.Wpf;component/Themes/Leds.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<ResourceDictionary x:Key="WindowTemplates">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<ResourceDictionary x:Key="ControlTemplates">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/QCast;component/Libraries/ControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Put all your ResourceDictionary's to the property ResourceDictionary of Application.Resources:
<Application.Resources>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.FontSize" Value="14"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SteelBrush.xaml"/>
<ResourceDictionary Source="FullSteel.xaml"/>
<ResourceDictionary Source="/MindFusion.Gauges.Wpf;component/Themes/Leds.xaml" />
<ResourceDictionary Source="Libraries/WindowsTemplates.xaml" />
<ResourceDictionary Source="/QCast;component/Libraries/ControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Related
I have a Wpf Application with the following structure
MainSolution -> Views (folder) -> NextGenDG (WPF Project) ->DisplayResources(folder) -> BRUserControlStyles.xaml
Here is BRUserControlStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NextGenDG.DisplayResources">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRColors.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRFonts.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRButtonStyles.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRCommonControlStyles.xaml" />
<ResourceDictionary
Source="/NextGenDG;component/DisplayResources/BRToggleButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style
x:Key="UserControlDarkBackgroundStyle"
TargetType="{x:Type UserControl}"
BasedOn="{StaticResource {x:Type UserControl}}">
<Setter
Property="Margin"
Value="0" />
<Setter
Property="Padding"
Value="0" />
<Setter
Property="Background"
Value="Red" />
<Style.Resources>
<Style
TargetType="{x:Type Button}"
BasedOn="{StaticResource ButtonLightTextGradientDarkBackgroundStyle}" />
<Style
TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource TextBlockLightTextStyle}" />
<Style
TargetType="{x:Type ToggleButton}"
BasedOn="{StaticResource ToggleButtonLightTextGradientDarkBackgroundStyle}" />
<Style
TargetType="{x:Type Separator}"
BasedOn="{StaticResource SeparatorDarkLineStyle}" />
</Style.Resources>
</Style>
</ResourceDictionary>
Now I am trying to use the style inside a UserControl that is inside a folder called UserControls as follows. This UserControl is being called in the MainWindow
<UserControl x:Class="NextGenDG.UserControls.NextGenHeaderCartridgeStatusUserControl"
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:NextGenDG.UserControls"
mc:Ignorable="d"
Style="{DynamicResource UserControlDarkBackgroundStyle}"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Grid.Column="1"
Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</UserControl>
Problem is if I say StaticResource UserControlDarkBackgroundStyle it complains saying the resource is not found. So when I use DynamicResource UserControlDarkBackgroundStyle it dosen't apply the style. Please help. At a glace is my ResourceDictionary path correct?
I have created a user control class library and I used a ResourceDictionary file in it.
Now, I want to use my usercontrol in a WPF application, but I have to add ResourceDictionary file again in my projet! If I don't add it, it brings the ResourceDictionary file, and show an error on MergeDictionaries block!
Am I missing something!?
Resource dictionary is:
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
<Rectangle Fill="Transparent" Cursor="Hand"/>
</ControlTemplate>
<Style x:Key="ItemStyle" TargetType="ContentControl">
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualWidth}"/>
<Setter Property="MinHeight" Value="60"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Content" Value="MyTextBox"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<s:MoveThumb Template="{DynamicResource MoveThumbTemplate}"/>
<ContentPresenter Name="MainControl" Content="{TemplateBinding ContentControl.Content}"
Margin="5,0,10,0"/>
<Grid Opacity="0" Margin="-3">
<s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<s:ResizeThumb Height="3" Cursor="SizeNS" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
adding to user control:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/MoveResizeThumb.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Give this a try:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Just to clearify the answer of #Willem: when having additional resources after the merged dictionary, there may appear the error "x:key attribute is required". In this case, those resources have to be inside the resource dictionary:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary source="/assembly;component/resource.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBlock" BasedOn="{StaticResource SetupTextBlockStyle}" />
<ResourceDictionary>
</UserControl.Resources>
<Grid />
In response to #ThisHandleNotInUse and #OliverAssad comments in the accepted answer.
In case of x:Key attribute required error, the ResourceDictionary tag should be modified as follows:
<UserControl.Resources x:Key="myKey">
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MoveResizeThumb.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Another way is to add the resource at an application level
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ProjectStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I want to disable (globally) the FocusVisualStyleKey.
I'm looking for a possibility, that I have not put on any element the following code:
<Style TargetType="ToggleButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
So I read this article, and found this solution:
App.xaml
<Application x:Class="WpfApplication7.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="{x:Static SystemParameters.FocusVisualStyleKey}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle StrokeThickness="0" SnapsToDevicePixels="true" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="WpfApplication7.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>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="98,230,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="217,230,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="368,230,0,0" VerticalAlignment="Top" Width="75"/>
<ComboBox HorizontalAlignment="Left" Margin="40,40,0,0" VerticalAlignment="Top" Width="150" Height="40"/>
<ComboBox HorizontalAlignment="Left" Margin="317,40,0,0" VerticalAlignment="Top" Width="150" Height="40"/>
</Grid>
</Window>
But it doesn't work...
Does anyone have another idea?
Yes, there is a way to do this but you are not going to like it. First make another xaml dictionary. In it define a style for each control in Aero or w/e theme you're using, then set BasedOn property to implicit key.
example
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
then create a setter for FocusVisualStyle
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
Note: you'll have to do this for EVERY control.
Next merge your new themes dictionary into Window.Resources
<Window.Resources>
<ResourceDictionary Source="[your dictionary]"/>
</Window.Resources>
When I add a resource on my main application (Application.xaml) then my pages doesn't load its design viewer because it tells me there is an error in one resource (DesignerItem.xaml). Otherwise in run time it works perfectly.
Application.xaml:
<Application xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Class="GesHoras.App"
StartupUri="WinMain.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Vista Theme -->
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
<ResourceDictionary Source="Themes/TabControl.xaml"/>
<ResourceDictionary Source="Themes/TabItem.xaml"/>
<ResourceDictionary Source="Themes/GridView.xaml"/>
<ResourceDictionary Source="Themes/ListView.xaml"/>
<ResourceDictionary Source="Themes/Button.xaml"/>
<ResourceDictionary Source="Themes/RepeatButton.xaml"/>
<ResourceDictionary Source="Themes/GroupBox.xaml"/>
<ResourceDictionary Source="Themes/TextBox.xaml"/>
<ResourceDictionary Source="Themes/ComboBox.xaml"/>
<ResourceDictionary Source="Themes/TreeView.xaml"/>
<!--<ResourceDictionary Source="Themes/Menu.xaml"/>-->
<ResourceDictionary Source="Themes/ProgressBar.xaml"/>
<ResourceDictionary Source="Themes/ToolTip.xaml"/>
<ResourceDictionary Source="Designer_Controls/DesignerItem.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
and my DesignerItem.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GesHoras.Designer_Controls">
<!-- Resource dictionary entries should be defined here. -->
<!-- DragThumb Default Template -->
<Style TargetType="{x:Type s:DragThumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:DragThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- DesignerItem Style -->
<Style TargetType="{x:Type s:DesignerItem}">
<Setter Property="MinWidth" Value="20"/>
<Setter Property="MinHeight" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:DesignerItem}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!-- PART_DragThumb -->
<s:DragThumb x:Name="PART_DragThumb" Cursor="Hand"/>
<!-- PART_ContentPresenter -->
<ContentPresenter x:Name="PART_ContentPresenter"
Content="{TemplateBinding ContentControl.Content}"
Margin="{TemplateBinding ContentControl.Padding}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
It tells me the error is in DesignerItem.xaml in line:
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
the message error that appears is:
Property 'Path' has no value
Can anyone help me please?
Thanks!
DesignerItem.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GesHoras.Designer_Controls">
<!-- Resource dictionary entries should be defined here. -->
<!-- DragThumb Default Template -->
<Style TargetType="{x:Type s:DragThumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:DragThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- DesignerItem Style -->
<Style TargetType="{x:Type s:DesignerItem}">
<Setter Property="MinWidth" Value="20"/>
<Setter Property="MinHeight" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:DesignerItem}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!-- PART_DragThumb -->
<s:DragThumb x:Name="PART_DragThumb" Cursor="Hand"/>
<!-- PART_ContentPresenter -->
<ContentPresenter x:Name="PART_ContentPresenter"
Content="{TemplateBinding ContentControl.Content}"
Margin="{TemplateBinding ContentControl.Padding}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
The problem is in line:
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
It says to me that path is missing or has no value.
Thanks!
I have a UserControl with a Border element within it that I want to style with a particular Border style. It compiles but won't start, giving a XamlParseException, saying, "Cannot find resource ..."
Is there a way to do this?
Thanks.
App.xaml:
<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
<Style x:Key="FancyBorder"
TargetType="{x:Type Border}">
<Setter Property="Margin" Value="0,0,0,8"/>
<Setter Property="Padding" Value="8"/>
...
</Style>
</Application.Resources>
MainView.xaml:
<Window x:Class="WahnamProgressTracker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:uc="clr-namespace:WahnamProgressTracker.UserControls"
MinHeight="500" MinWidth="800">
<DockPanel>
<uc:MainViewMenu x:Name="menu"
DockPanel.Dock="Top" />
<StatusBar x:Name="quoteBar"
DockPanel.Dock="Bottom">
<TextBlock Text="{Binding Path=Quote.Text, Mode=OneWay}" />
</StatusBar>
<uc:MainViewNavigation x:Name="navigationBar"
DockPanel.Dock="Left" />
<uc:ProgressGraph x:Name="graph" />
</DockPanel>
MainViewNavigation.xaml (user control):
<UserControl x:Class="WahnamProgressTracker.UserControls.MainViewNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border Style="{StaticResource FancyBorder}">
...
</Border>
</UserControl>
Can you post a sample of what you mean? The only case in which your issue can occur is if the User Control is created and then rendered outside your application's visual tree.
The XAML below works for me:
App.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Application.Resources>
</Application>
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<local:UserControl1 />
</Grid>
</Window>
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TextBlock Style="{StaticResource myStyle}">HEY!</TextBlock>
</Grid>
</UserControl>