Databinding Enum to ComboBox - wpf

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.

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.

How to add designmode dummy data into WPF control

Added entity Dummy to the usercontrol resources but Visual Studio complains it can't find resource 'Dummy'. Is it possible to add design data this way? What am I doing wrong?
<UserControl x:Class="MovieScraper.Media"
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:entity="clr-namespace:Processor.Entity;assembly=Processor"
mc:Ignorable="d"
d:DataContext="{StaticResource ResourceKey=Dummy}"
d:DesignHeight="300" d:DesignWidth="300" Background="White">
<UserControl.Resources>
<entity:Media x:Key="Dummy" Title="Akira"></entity:Media>
</UserControl.Resources>
<Grid>
<TextBlock Text="{Binding Title}"></TextBlock>
</Grid>
</UserControl>
I would create a MockDataContext model. This works pretty nice. This is a snippet from code that I have:
<UserControl x:Class="Modules.Core.Views.HeaderView"
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:viewModels="clr-namespace:Modules.Core.ViewModels"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="1024">
<Grid d:DataContext="{d:DesignInstance Type=viewModels:MockHeaderViewModel, IsDesignTimeCreatable=True}">
</Grid>
</UserControl>
Big advantage is that you can actually run some code. For example I use it to update the time in my header in design time and vary some fields. You can immediately see if your binding work and your layout is giving you components enough space.
You are trying to bind d:DataContext to resource Dummy, but you are missing the keyword Binding. Please change this line to
d:DataContext="{Binding Source={StaticResource Dummy}}

Can UserControl.Resources reference a different assembly than the control's contents?

I'm having a bizarre problem with one of my UserControls where if I only make use of a xml namespace in the UserControl.Resources the assembly isn't found. But if I also make use of the same xml namespace in the contents of the control then both work properly.
<UserControl x:Class="MyControl"
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:myAssemblyAlias="clr-namespace:MyAssembly;assembly=MyAssembly">
<UserControl.Resources>
<ControlTemplate x:Key="AControlTemplate">
<... other stuff .../>
<myAssemblyAlias:MyObject/>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<myAssemblyAlias:MyObject x:Name="MyObjectInContent" />
</Grid>
</UserControl>
If both instances of MyObject are present it works correctly. If I comment out the line for MyObjectInContent I get a runtime error telling me MyAssembly can't be found.
Anyone ever seen anything like this before? It almost seems like Visual Studio has the reference available properly at compilation but not at runtime.

WPF: Add null value as a static resource

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>

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>

Resources