WPF: Add null value as a static resource - wpf

Is it possible to add null as a static resource of a markup element? I want to be able to refer to a value using {StaticResource myKey} syntax. At the moment the value I need to refer to is null, but in future it might not be. I have multiple references to the value in the rest of the markup and I would like them to refer to a resource key rather than {x:Null}.
I expected to do this:
<Window.Resources>
<x:Null key="myKey" />
</Window.Resources>
...but that does not work. It compiles but at runtime a XamlParseException is raised saying that the resource reference cannot be resolved.

This works fine for me:
<Window x:Class="SO16456565.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>
<!--<SolidColorBrush x:Key="BG" Color="AntiqueWhite"/>-->
<x:NullExtension x:Key="BG"/>
</Window.Resources>
<Border Background="{StaticResource BG}"/>
</Window>

Related

Error XLS0502 The 'WindowsFormsHost' type does not support direct content

I'm trying to use a winform control in WPF(I've not found good alternative).
The control is the be.hexbox from sourceforge: https://sourceforge.net/projects/hexbox/files/hexbox/
So I Start a new solution VB.net WPF and add WindowsFormsIntegration.dll reference.
I also add reference to the control dll.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:Be.Windows.Forms;assembly=Be.Windows.Forms.HexBox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<WindowsFormsHost Name="TEST1">
<wf:HexBox x:Name="HX" />
</WindowsFormsHost>
</Grid>
</Window>
but I get this error:
Error XLS0502 The 'WindowsFormsHost' type does not support direct content.
Any advice?
Indeed, you can't set a direct Content in a WindowsFormsHost element, you need to set the Child property instead.
Try this:
<WindowsFormsHost Name="TEST1">
<WindowsFormsHost.Child>
<wf:HexBox x:Name="HX" />
</WindowsFormsHost.Child>
</WindowsFormsHost>
The project must have a reference to both:
WindowsFormsIntegration
System.Window.Forms
In my experience you do not need to specify <WindowsFormsHost.Child> in XAML. This may be dependent on the version. I am currently using .NET Framework 4.8.

WPF: Use PropertyGrid from WPF Extended

I'm new in WPF (.NET 4.0, VS2010) and try to include a property grid. My XAML Markup looks like the following:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="clr-namespace:Xceed.Wpf.Toolkit.PropertyGrid;assembly=WPFToolkit.Extended"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WpfToolkit:PropertyGrid Name="Grid" />
</Grid>
</Window>
Now I get the error that the assembly 'WPFToolkit.Extended' was not found. I have included the reference to the dll Xceed.Wpf.Toolkit.dll in my application.
Why does it not work? Have I to include more references or what can be the mistake?
Thanks for any Response.
I could solve the problem. My fault was that I had not unblocked the zip file containing the dll. After this I have to change the source for the namespace in the Markup to an uri. Below the final markup:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WpfToolkit:PropertyGrid Name="Grid" />
</Grid>
</Window>
Check your project properties. Under application, what is selected under target framework? By default with a new project, '.Net Framework 4 Client Profile' is selected. If the extended library uses parts of .net that beyond the client profile, you will need to select '.Net Framekwork 4' instead to use the extended functionality.

Including resource dictionary in UserControl.Resources fails to compile

I have a custom control that I want to apply some styles from a resource dictionary in another project. For some reason, adding resources in the UserControl.Resources fails, however placing it in the Grid.Resources succeeds;
<UserControl x:Class="My.Name.Space.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" d:DesignHeight="200" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary x:Key="UserControlResources">
<!--Putting it here fails-->
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.Resources>
<ResourceDictionary x:Key="GridResources">
<!--Putting it here succeeds-->
</ResourceDictionary>
</Grid.Resources>
<!--Control XAML-->
</Grid>
</UserControl>
I get an error "The name 'IntializeComponent' does not exist in the current context". However, looking at the autogenerated MyUserControl.g.i.cs file, hey presto; there it is. What am I doing wrong? What is the difference between these two declarations?
Thanks, Sterren

Referencing a Parent's ResourceDictionary in a UserControl

I have a library of WPF UserControls, and a ResourceDictionary that is shared within the library.
All the UserControls in this library appear only within a single 'shell' parent control, which is really just a container for a collection of smaller controls. I'm able to access the ResourceDictionary from my shell control as expected when I add the following XAML
<Control.Resources>
<ResourceDictionary Source="MyResources.xaml" />
</Control.Resources>
However I can't access the ResourceDictionary from child controls that sit inside the 'shell' control.
I was under the impression that WPF should check locally for resources, and then traverse upwards until appropriate resources are found?
Instead I'm getting
Cannot find resource named '{BoolInverterConverter}'.
Resource names are case sensitive. Error at
object 'System.Windows.Data.Binding' in markup file...
Obviously I can (and am) referencing the ResourceDictionary in my child controls; but each and every control now needs to reference this dictionary and I believed that this was not necessary.
Any ideas, am I doing something strange or is my expectation of behaviour incorrect?
What's going on is described here, though the documentation's a little opaque. If you add a ResourceDictionary to an element'sResources property without specifying a key, WPF expects that you're merging in resource dictionaries, and it populates the dictionary with the contents of the dictionaries in its MergedDictionaries property. It ignores the actual contents of the ResourceDictionary with no key.
So what you want to do is this:
<Control.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Control.Resources>
Edit:
A working example:
MainWindow.xaml:
<Window x:Class="MergedDictionariesDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MergedDictionariesDemo="clr-namespace:MergedDictionariesDemo" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<MergedDictionariesDemo:UserControl1 />
</Grid>
</Window>
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="UCBrush"
Color="Bisque" />
</ResourceDictionary>
UserControl1.xaml:
<UserControl x:Class="MergedDictionariesDemo.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Border Margin="10" BorderBrush="Navy" BorderThickness="1" CornerRadius="10">
<TextBlock Margin="10"
Background="{DynamicResource UCBrush}">
The background of this is set by the resource UCBrush.
</TextBlock>
</Border>
</UserControl>

Databinding Enum to ComboBox

I'm getting a very unusual error on a project that use to work where I'm trying to bind an ENUM to a combo box. To ensure I've not made coding errors, I've made a new usercontrol using SO Question 58743 and ageektrapped as samples for a self contained user control. I'm using .Net4 Client Framework as the environment and VS2010. The xaml is -
<UserControl x:Class="Barcode.Views.UserControl1"
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:System="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type System:Enum}"
x:Key="AlignmentValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="HorizontalAlignment" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<Grid>
<ComboBox Name="myComboBox" SelectedIndex="0" Margin="8"
ItemsSource="{Binding Source={StaticResource AlignmentValues}}" />
</Grid>
</UserControl>
The error that I'm getting on the ComboBox is Error 144 Unable to cast object of type 'System.String' to type 'System.Windows.DataTemplate' after building the project.
I'm at a loss as to what could be causing this error.
Looks like you may be missing the namespace on your HorizontalAlignment. Add a relevant namespace where the HorizontalAlignment type resides.
xmlns:local="clr-namespace:Barcode.Views"
Then modify your XAML to make use of the newly defined namespace...
...
<x:Type TypeName="local:HorizontalAlignment"/>
...
EDIT:
With this being the HorizontalAlignment enum type from within the framework then your code should work as is. I tested it to be certain and it indeed works; as I placed an instance of the UserControl on my Window and it worked without a hitch. Set up an empty project and start from scratch to see if the problem still exists as you may have other factors causing the issue.

Resources