WPF Reference control in resourcedictionary - wpf

I created my resource dictionary and can access styles in it, but how (If possible) would I reference an entire control from it?
Here's the resourcedictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MainWindowStyle" TargetType="Window">
<Setter Property="Background" Value="Transparent"></Setter>
<!--Can access this-->
</Style>
<Window x:Key="CustomWindow" Background="Transparent">
<!--Want to access this-->
</Window>
</ResourceDictionary>
Here's the main window xaml file:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="640" Width="960" >
<!--setting Style={StaticResource MainWindowStyle} adds the style but I want this window to be replaced by the one in the resourcedictionary-->
<Grid>
</Grid>
</Window>
So I basically want to replace the window in the xaml file with the one created in the resource dictionary.

You can certainly host something like a UserControl within another control.
E.g. a ContentControl
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window x:Key="CustomUserControl" Background="Transparent">
<!--Want to access this-->
</Window>
</ResourceDictionary>
Then:
<ContentControl Content="{StaticResource ResourceKey=CustomUserControl}" />
I suspect that's not quite what you're after, but it's not clear what you may/may not have within the Window that's of interest.

Related

Style an activation indicator from material design

I want to style an activation indicator from material design in WPF. I want the styling to happen in an external styling.xaml file, so the textbox I'm using can be binded to that style.
The textbox:
<TextBox materialDesign:HintAssist.Hint="Search:" Grid.Column="1" Margin="35 8 20 5"
Width="200" Style="{"STYLE style"}/>
You could set it as a StaticResource:
File: styling.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace">
<Style TargetType="{x:Type TextBox}" x:key="YourCustomTextBoxStyle">
<Setter Property="Margin" Value="35 8 20 5"/>
....
</Style>
</ResourceDictionary>
In your View (or Application) add a reference to the styling ResourceDictionary and use the style as a static resource:
<UserControl
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:YourNamespace.Wpf"
x:Class="YourView" mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourNamespace;component/Themes/styling.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<TextBox materialDesign:HintAssist.Hint="Search:" Grid.Column="1" Width="200" Style="{StaticResource YourCustomTextBoxStyle}/>
</UserControl>

how to disable cut, copy paste and right click across all WPF forms

We are building a WPF kiosk application.
We need to disable CUT COPY PASTE and RIGHT-CLICKs
Please how can this be done?
This SO post does not give a centralized solution for all forms:
How to suppress Cut, Copy and Paste Operations in TextBox in WPF?
You need to add a Style in your App.xaml in which you define:
<Style TargetType="TextBox">
<!-- OR -->
<Style TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>
But this will only work for the Items that are NOT in a DataTemplate.
UPDATE:
App.xaml:
<Application x:Class="TestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>
</Application.Resources>
And here is the MainWindow.xaml:
<Window x:Class="TestApp.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"
xml:lang="en-GB"
xmlns:local="clr-namespace:TestApp"
xmlns:converter="clr-namespace:TestApp.Converters"
mc:Ignorable="d"
Height="478.889" Width="903.889">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel>
<TextBox Name="txtBx" MinHeight="150"
VerticalAlignment="Top" AutoWordSelection="True"
MaxLines="10"
TextWrapping="WrapWithOverflow"
SelectionChanged="txtBx_TextHighlighted"
ToolTip="{x:Null}"
Margin="10"/>
</StackPanel>
</Grid>
If you Right Click on the TextBox you will not have any ContextMenu available to you.
UPDATE 2:
Continuing from our chat, the TextBox was referencing other styles which were overriding whatever we set in the App.xaml. As the external styles were loaded after the App.xaml.

Split TabItem cause style lost

I've a TabControl with this structure:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</TabControl.Resources>
<local:Analysis />
</TabControl>
Now I need to split each TabItem, so I've create something like a UserControl with this structure:
<TabItem 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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MetroTabItem}">
<Grid>
</Grid>
the problem's that the TabItem imported with namespace local: doesn't take the style of the TabControl in the Resource.
So for solve this I need to put the style resource in each TabItem, this will cause redundancy code.
How can I take the style of TabControl Resource in the splitted TabItem?
Thanks.
You can create your own style as a static resource in your App.xaml or your Window.xaml like this:
<Application.Resources>
<Style TargetType="TabItem" x:Key="MyTabItemStyle" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</Application.Resources>
You give your style a name (MyTabItemStyle in this example) which is based on the MetroTabItem style.
Then, instead of using the MetroTabItem style directly in your custom TabItem, you use your own style MyTabItemStyle like this:
<TabItem 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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MyTabItemStyle}">
<Grid>
</Grid>
</TabItem>

ResourceDict share & set Template

I will use XAMLReader to load wpf screens.
I have some ControlTemplates on Buttons.xaml file and i am keeping it in a common class library (ClassLibrary1).
Buttons.xaml is like that:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="LeftButton" TargetType="{x:Type Button}">
// Some codes
</ControlTemplate>
<ControlTemplate x:Key="RightButton" TargetType="{x:Type Button}">
// Some codes
</ControlTemplate>
</ResourceDictionary>
On main WPF project, this dll is added as reference.
My xaml is like that:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="600">
<Grid>
<Grid.Resources>
<ResourceDictionary x:Key="testSource">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="s" Source="pack://application:,,,/ClassLibrary1;component/Buttons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<Button Template="{DynamicResource RightButton}" Content="TestBtn" HorizontalAlignment="Left" Width="100" Height="100" Margin="474,529,0,0" ></Button>
</Grid>
</UserControl>
When i use Template="{DynamicResource RightButton}" template is not loaded correctly (actually there is no sign of button on screen).
Also there is no error about loading the resourcedictionary.
I couldn't find, what should i use in order to load template on this button. Can anyone help me?

Loading WPF Style from Resource File

I am trying to load WPF Style from other file actually from WPF Custom Control Library
but i am failing to load here is my solution.
The solution contains two projects
WpfTestControls of Type WPF Custom Control Library
WpfTestApp of type WPF Application Library which has reference to WpfTestControls
MainWindow.xaml from WPF Application Library
<Window.Resources>
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Height="50px" Width="100px" Style="{DynamicResource TempStyle}"/>
</Grid>
Generic.xaml from WPF Custom Control Library
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfTestControls;component/TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
TextBoxStyle.xaml from WPF Custom Control Library
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
My AssemblyInfo.cs file contains the following
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries))]
But still i am failing to load the Style.
If i am using the not using the Generic.xaml everything work fine for example the following code works as expected
<Window x:Class="WpfTestApp.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 x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Height="50px" Width="100px" Style="{StaticResource TempStyle}"/>
</Grid>
What am i doing wrong ?
Thanks in advance
Please answer few things for me...
Is "WPF Custom Control Library" assembly same as "WpfTestControls" assembly?
If not, then does "WPF Custom Control Library" have a reference to the "WpfTestControls" assembly?
Does your WpfTestApp have a reference to both "WPF Custom Control Library" and "WpfTestControls" assemblies?
If you add that reference(s), the resources should load correctly.
My Steps...
Add a "WPF Custom Control Library" say "ThemesLibray"
In this add two resource dictionaries under "Themes" folder
TextBoxStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="GreenTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I have main starup project "MyWPFTestApp" that has assembly reference to ThemesLibray. In that the window has ThemesLibrary resources merged this way....
<Window x:Class="MyWPFTestApp.Window7"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window7" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/ThemseLibrary;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<TextBox Style="{StaticResource GreenTextBoxStyle}"/>
</Grid>
</Window>
When I launch MyWPFTestApp, I see the Window with green TextBox.
Main thing is : Make sure you have Build Action of your Resource Dictionary set to Resource.

Resources