WPF - Update static resource value at runtime - c#

I have code similar to the following:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Software_Suite_Maker"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="WpfApplication1.App"
StartupUri="MainWindow.xaml">
<Application.Resources>
<FontFamily x:Key="FontFamilyName">./Fonts/#Segoe UI</FontFamily>
</Application.Resources>
and the Window xaml code is:
<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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox FontFamily="{StaticResource FontFamilyName}" Margin="135,122,187,180" Text="test"/>
<Button FontFamily="{StaticResource FontFamilyName}" Margin="135,144,329,154" Content="test"/>
</Grid>
Now I want to change the value of FontFamilyName from behind code. I wrote this code:
var font = TryFindResource("FontFamilyName") as FontFamily;
font = new FontFamily("./Fonts/#Tahoma");
But nothing happened and did not change.
My question is: How can I change FontFamilyName value from behind code and changes will also be made on the objects?

You have to use a DynamicResource for that :
<TextBox FontFamily="{DynamicResource FontFamilyName}" Margin="135,122,187,180"
Text="test"/>
<Button FontFamily="{DynamicResource FontFamilyName}" Margin="135,144,329,154"
Content="test"/>
Read on MSDN about DynamicResource:
Provides a value for any XAML property attribute by deferring that value to be a reference to a defined resource. Lookup behavior for that resource is analogous to run-time lookup.

Related

WPF: Can't change property of custom control inside other control template (+Pictures)

Let's create an empty WPF project.
Add a very simple UserControl (i named it MyUserControl):
<UserControl x:Class="Delete_This_Test.MyUserControl"
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"
mc:Ignorable="d"
Background="Red"
Height="100">
<Grid>
</Grid>
</UserControl>
As you can see, i have changed only 2 properties: Background and Height to "Red" and "100".
Put our created control in MainWindow:
<Window x:Class="Delete_This_Test.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:Delete_This_Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:MyUserControl Width="100"
Height="200"
Background="Blue">
</local:MyUserControl>
</Grid>
</Window>
Here, i have changed Width, Height and Background to "100", "200" and "Blue".
And it works: Without ControlTemplate Picture
But if we put MyUserControl in some ControlTemplate, for example of Button:
<Window x:Class="Delete_This_Test.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:Delete_This_Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<local:MyUserControl Width="100"
Height="200"
Background="Blue">
</local:MyUserControl>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>
This will not work. Only Width property will changed, because we didn't set it in MyUserControl xaml.
Height and Background will be the same as "100" and "Red":
With ControlTemplate Picture
So my question is: Is it bug of WPF, or i'm missing something obvious?
*
Because i need to use one custom control in different templates, and change some properties, e.g. Background of control

ColorZone doesn't drop shadow in WPF MaterialDesign

I try to make ColorZone in WPF using the MaterialDesign library. But when I set
materialDesign:ShadowAssist.ShadowDepth="Depth5"
it doesn't show any shadow. Below is a full code of the sample app where it doesn't work. Can anyone tell me what I am doing wrong?
I added MaterialDesignThemes to the project.
In app.xaml paste this:
<Application
x:Class="TestShadow.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestShadow"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Light"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />
<ResourceDictionary
Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And to MainWindow.xaml I added:
<Window
x:Class="TestShadow.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:TestShadow"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<materialDesign:ColorZone
Mode="PrimaryDark"
Padding="16"
materialDesign:ShadowAssist.ShadowDepth="Depth5">
<TextBlock
Text="Material Design In XAML Toolkit"
VerticalAlignment="Center" />
</materialDesign:ColorZone>
</Grid>
</Window>
Result, no shadow:
Apparently there is some problem for the MaterialDesign current version. The only way I have found is to place the Card element under the ColorZone. In this case, the shadow becomes visible.

XAML: The property "Content" can only be set once - though already only set once

I'm trying to set up a material design in a C# XMAL WPF application.
I added "Material Design In XAML" to my project, after which I got the following error: The property 'Content' can only be set once where would be the problem in my mark up? I've provided the file which gives the error below:
<Window x:Class="e621_fetch.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:e621_fetch"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}">
<Grid>
<materialDesign:Card Padding="32" Margin="16"> //error on this line
      
<TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}">My First Material Design App</TextBlock> //error on this line
    
</materialDesign:Card> //error on this line
</Grid>
already fixed it, it was because of the blank lines in between the tags...

Window in Window Dynamic Content

I am a little new to WPF.
I want to use one window for all my content, the content will vary dramatically and the Window.Resources content will need to vary also.
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Grid>
...
</Grid>
</Window>
As an example, I would like to use some third party content like FluidKit's ElementFlow.
There are many good examples of how to dynamically Load UserControls, but nothing on Window in Window. The best I have found is: Changing content dynamically in wpf window
I would like to have multiple *.xaml Files in my project and load content dynamically into the Current Window.
EDIT:
I like WPF Frame, but I can not load and unload Window.Resources:
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Frame Name="ContentHolder" />
</Window>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
Just so you know, I have explored:
SolidColorBrush SolidColorBrushRed = new SolidColorBrush(Colors.Red);
this.Resources.Add("RedBrushResource", SolidColorBrushRed);
Is what I am wanting to do possible? Can I load all the required content from a single xaml File?
Thanks.
EDIT:
This is one method, but not really what I was wanting:
<Page x:Class="NS01.XAML.Repository.Page1"
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:NS01.XAML.Repository"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<TextBlock Text="Welcome" FontSize="18" Foreground="{DynamicResource ResourceKey=textForeColorResource}" Margin="80,32,66,219"/>
</Grid>
</Page>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NS01.XAML.Resources">
<SolidColorBrush x:Key="textForeColorResource" Color="Blue"/>
</ResourceDictionary>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
ResourceDictionary RD = new ResourceDictionary()
{
Source = new Uri("/NS01;component/XAML/Resources/Page1.xaml", UriKind.RelativeOrAbsolute)
};
Application.Current.Resources.MergedDictionaries.Add(RD);
Application.Current.Resources.MergedDictionaries.Remove(RD);
foreach (var R in MWindow.Resources)
{
MessageBox.Show(R.ToString());
}

Get XAML code in WPF

How can I get XAML code of a window as a string, change this string, and upload this string as a xaml-code of window?
For example, I have
<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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132"></ComboBox>
</Grid>
</Window>
and I want to have this xaml code in the end
<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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="0,0,9,38" Name="button1" Height="82" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="132">
<ComboBox.ItemTemplate>
<DataTemplate>
<local:d_dddw_imp_insurance_list />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
If your using wpf/xaml with c#, visual studio generally compile the xaml code to c#, to be as performant as possible. If you want to have some dynamic XAML, you need to use XamlReader to read xaml and display at "on your own".
Just take a look at: Loading XAML XML through runtime
Than you can change or manipulate the xaml and display the result very easily.

Categories

Resources