WPF Controls as StaticResource in Resource Dictionary, used in multiple WPF Windows? - wpf

I have a Button control as a resource in Resource Dictionary as below:
<!--ButtonResources.xaml file-->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Key="buttonResource" Content={Binding BoundText}/>
</ResourceDictionary>
<!--ButtonResources.xaml file-->
I now use this above button control bound to Content property of ContentControl controls in 2 different Windows .xaml files where each Window has its own DataContext and thus each window should display the Content of above button control based on its ViewModel's BoundText property value as below for each Window.
<Window x:Class="TestClass1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ButtonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content={StaticResource buttonResource}/>
</Grid>
</Window>
But, the problem is that both Window's display same value for the BoundText property which means that both the WPF Windows have same instance of button control from resource, used in both the Windows.
How can I resolve this problem such that each Window gets a separate button control from the resource and still display different values for the BoundText property from their own ViewModel?
Edit:
For the reason mentioned in MSDN as below, I can't use x:Shared="False" attribute to resolve this:
•The ResourceDictionary that contains the items must not be nested
within another ResourceDictionary. For example, you cannot use
x:Shared for items in a ResourceDictionary that is within a Style that
is already a ResourceDictionary item.

Did you try to use the x:Shared attribute?
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Shared="False" x:Key="buttonResource" Content={Binding BoundText}/>
</ResourceDictionary>
For more info read here.
In case this does not work, you can store a template in your resource, instead of the button and use a ContentControl inside your window to display it.

Try:<Style TargetType="Button" x:Key="buttonResource">
<Setter Property="Content" Value="{Binding BoundText}" />
</Style>
<Button Style="{StaticResource buttonResource}" />

Related

Only last icon in XAML shown when icon used from resources

when I try to use a material design icon from the icon pack that is defined in the ResourceDictionary, only the first icon in XAML is rendered at run time. I've followed an example that can be found here.
Example follows:
App.xaml:
<Application x:Class="TestWpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpf"
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 Source="pack://application:,,,/TestWpf;component/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
<materialDesign:PackIcon x:Key="CashIcon" Kind="Cash" />
</ResourceDictionary>
MainWindow.xaml:
<Window x:Class="TestWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Content="{DynamicResource CashIcon}" />
<Button Content="{DynamicResource CashIcon}" />
<Button Content="{DynamicResource CashIcon}" />
</StackPanel>
</Window>
And the result is a window that looks like this:
In xaml editor all buttons have icons on them, as expected:
Why is this happening and, more important, how to fix this?
P.S. We've found out that if you create two Cash icons in the ResourceDictionary and apply each to a button, then they will both be shown but again, only once, you can't have say 3 buttons and 2 icons in ResourceDictionary.
one more solution is to use a non-shared resource (x:Shared Microsoft docs)
<materialDesign:PackIcon x:Key="CashIcon" Kind="Cash" x:Shared="False"/>
x:Shared Attribute: When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.
A scenario for x:Shared="false" is if you define a FrameworkElement or FrameworkContentElement derived class as a resource and then you introduce the element resource into a content model. x:Shared="false" enables an element resource to be introduced multiple times in the same collection (such as a UIElementCollection). Without x:Shared="false" this is invalid because the collection enforces uniqueness of its contents. However, the x:Shared="false" behavior creates another identical instance of the resource instead of returning the same instance.
The PackIcon type is a Control. An element in the visual tree in WPF can only have a single parent. In other words, the pack icon is still a single instance added as child of the first button, then moved to the second, then to the third. You will in fact have to create multiple instances of the pack icon.
Instead of creating resources, you could use the PackIcon markup extension.
<StackPanel>
<Button Content="{materialDesign:PackIcon Cash}"/>
<Button Content="{materialDesign:PackIcon Cash}"/>
<Button Content="{materialDesign:PackIcon Cash}"/>
</StackPanel>
Depending on your actual scenario, you could alternatively create a DataTemplate, which will automatically create instances of the pack icons for each button.
<DataTemplate x:Key="CashPackIconTemplate">
<materialDesign:PackIcon Kind="Cash" />
</DataTemplate>
<StackPanel>
<Button ContentTemplate="{StaticResource CashPackIconTemplate}"/>
<Button ContentTemplate="{StaticResource CashPackIconTemplate}"/>
<Button ContentTemplate="{StaticResource CashPackIconTemplate}"/>
</StackPanel>

Setting style on/in UserControl without having to define MergeDictionary?

I'm looking to set a style on some UserControls that's kept in a resource dictionary at the base of my project tree. The only way I've found to do so is below by defining MergeDictionary in the resources to link in the xaml file with styles. I'd prefer to not have to put this same code on every single control that needs this style though. Is there a better way to do this?
<UserControl
x:Class="TestApp.Screens.Sub.Details"
...
Style="{DynamicResource BottomContentUserControl}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../GeneralStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
</UserControl>
You can add the styles to the ApplicationResourceDictionary or you could add them to your App.xaml file so that they will be applied throughout your application.

Style defined in ResourceDictionary referencing a template within the same ResourceDictionary

I have created a ResourceDictionary in my WPF application like so:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfRibbonApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:vsm="http://schemas.microsoft.com/netfx/2007/xaml/presentation" >
</ResourceDictionary>
Inside of this resource dictionary I have defined a style for my custom textbox control like so:
<Style TargetType="{x:Type local:SimpleSwingTextBox}" x:Key="SimpleSwingTextBoxStyle">
</style>
Within the style is a tooltip:
<ToolTip x:Name="validationTooltip" Template="{StaticResource ValidationToolTipTemplate}">
</Tooltip>
Also within the same ResourceDictionary I have the template referenced above in the style:
<ControlTemplate x:Key="ValidationToolTipTemplate">
</ControlTemplate>
Then in my C# code, I create an instance of my custom text box control and set its style to the style defined in the resource dictionary:
SimpleSwingTextBox textBox = new SimpleSwingTextBox(textPoint);
Uri resourceLocator = new Uri("Assets/SimpleSwingResourceDictionary.xaml", System.UriKind.Relative);
ResourceDictionary rd = (ResourceDictionary)Application.LoadComponent(resourceLocator);
textBox.Style = rd["SimpleSwingTextBoxStyle"] as Style;
The problem I have is that an exception is immediately thrown which has the following message:
"Cannot find resource named 'ValidationToolTipTemplate'. Resource names are case sensitive."
I don't understand how the style I created cannot find the 'ValidationToolTipTemplate' when they are in the same ResourceDictionary xaml file?
Can anyone see what I am doing wrong here? Thanks

Set window icon in style

I would like to use the same icon for my main window and for any dialogs or message boxes whithin my application, so I tried to set it like this in a ResourceDictionary:
<Style TargetType="{x:Type Window}">
<Setter Property="Icon" Value="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></Setter>
</Style>
But that does not work.
How could I share the same icon with the different windows?
Edit:
I have a simple resource dictionary (Style.xaml) where I am defining some global settings. I use it in my App.xaml like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/ViewModelTemplates.xaml"/>
<ResourceDictionary Source="Resources/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The file contains some definitions like e.g. button height, text box foreground color, etc.
There is no problem with those and all the panels and window my application creates use these settings. That is why I would like the icon to be defined there as well, to have it used allover the application.
I am not looking for a way to set the icon of the .exe file.
Edit:
I have not found the solution for what I want to do, so I ended up creating a BitmapImage in my ResourceDictionary and use it as DynamicResource in each of my Window-Classes.
<BitmapImage x:Key="ApplicationIcon" UriSource="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></BitmapImage>
and
<Window ...
Closing="Window_Closing"
Title="{Binding Title, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding IsEnabled, FallbackValue=True, Mode=OneWay}"
WindowState="Maximized"
Icon="{DynamicResource ApplicationIcon}">
...
</Window>
For others that come across this issue, as I had the same one, see a similar question which provides a bit more of an explanation to why this doesn't work.
How to set default WPF Window Style in app.xaml?
There's also a couple of suggestions; one being tabina's solution and to apply the style to each Window separately. It includes an interesting approach to deriving the Window class, but it's down to personal preference, as they're only work-arounds.

How to add controls on dialog which has predefind WPF theme?

I am new to WPF. I want to do something like this:
I have multiple dialogs in an application. I have created one theme with the required background, title bar and close button I want for all dialogs. Whereas, all dialogs size, and controls for them will differ.
For example: DialogWindow is a theme I have created for Window control.
On MainWindow (where I apply this DialogTheme), I can see this theme.
But when I try to add controls on it, they do not show up on the theme.
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="391" Width="616"
Style="{DynamicResource DialogWindow}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/DialogsTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</Window>
I am not sure, what is going wrong. Any help will be appreciated.
You need to add these lines to each window you create to get achieve the style...
<Window...Style="{DynamicResource DialogWindow}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/DialogsTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Finally got an answer, ContentPresenter is a thing required, which works as client area for window theme.
<ContentPresenter Grid.Row="1" Grid.ColumnSpan="2" x:Name="ClientArea" />

Resources