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

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.

Related

The property "DataContext" does not exist in the "http://schemas.microsoft.com/expression/blend/2008" namespace

I'm using a DesignTime DataContext inside of my WPF Styles to get full IntelliSense support.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:alarm="clr-namespace:Foo.Alarm;assembly=HtCore"
mc:Ignorable="d">
<Style TargetType="TreeViewItem" d:DataContext="{d:DesignInstance alarm:HtAlarmBase}">
</Style>
</ResourceDictionary>
But the Designer highlights it and says:
The property "DataContext" does not exist in the "http://schemas.microsoft.com/expression/blend/2008" namespace
Is there a solution to hide this "error message"?
You could try:
<Style TargetType="TreeViewItem">
<d:Style.DataContext>
<x:Type Type="alarm:HtAlarmBase" />
</d:Style.DataContext>
</Style>
I don't really follow where you're headed with this though.
I would usually provide a design time datacontext for a whole view, including data for the treeview items. Without that, I wouldn't have any treeview items at all to show in the designer.
Personally, I had a similar error where it was complaining about Style not existing and I just had to switch xmlns:d="http://schemas.microsoft.com/expression/blend/2008" to xmlns:d="http://schemas.microsoft.com/expression/blend/2010" for it to compile again.
Do you have this line in your code?
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
If yes, try to clean (build > clean) your solution.

ControlTemplate TargetType does not match templated type

I have created this usercontrol in WPF
<UserControl x:Class="WpfApp1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<UserControl.Template>
<ControlTemplate TargetType="{x:Type local:UserControl1}">
<Grid x:Name="LayoutRoot" Background="White" Margin="7,7,7,0">
</Grid>
</ControlTemplate>
</UserControl.Template>
When I compile this I get the following error.
'UserControl1' ControlTemplate TargetType does not match templated type 'UserControl'.
But when I debug the application it works fine.
What does this error mean? And how do I fix it?
The error most likely only shows up when you have the XAML editor open, right? This is because it is a XAML Designer problem, not a WPF or .NET problem. That is why it works fine when you run, but shows an error in Visual Studio.
I am having similar issues because the XAML Designer also does not support polymorphism on ControlTemplates (template's TargetType="baseclass", but is applied to a derived class) - yet this also work at runtime.
The main workaround is to not define your custom control using XAML. Instead, create a templated control, where the template is selected by an external style resource. See http://mrbool.com/how-to-create-a-custom-control-in-xaml-and-c/26447 for more information.

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>

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.

Can the MVVM-Light ViewModelLocator be used in nested ViewModels?

The Visual Studio 2008 Designer doesn't seem to like UserControls that reference the MVVM-Light ViewModelLocator. I get an error message like:
Could not create an instance of type 'MyUserControl'.
For example, the following XAML will cause this behavior if MyUserControl uses the ViewModelLocator to establish its DataContext.
<Page x:Class="MyProject.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Views="clr-namespace:MyProject.Views"
>
<Grid>
<Views:MyUserControl/>
</Grid>
</Page>
MyUserControl is extremely simple:
<UserControl x:Class="MyProject.Views.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding MyNestedViewModel, Source={StaticResource Locator}}"
>
<Grid>
<TextBlock>Hello</TextBlock>
</Grid>
</UserControl>
And the "MyNestedViewModel" property simply instantiates an instance of the MyNestedViewModel class, which has absolutely no code in its default constructor.
Two questions:
Am I using the ViewModelLocator correctly? That is, can it be used in nested views or is it only meant for top-level Views?
Could this just be another bug in Cider, the Visual Studio 2008 designer?
Note that everything works perfectly at runtime. I only have problems at design time. But I hate coding XAML blind.
I encounter the same situation in VS 2010. A partial workaround I JUST discovered...
In your UserControl, change DataContext to d:DataContext
<UserControl x:Class="MyProject.Views.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DataContext="{Binding MyNestedViewModel, Source={StaticResource Locator}}"
>
<Grid>
<TextBlock>Hello</TextBlock>
</Grid>
</UserControl>
Unfortunately, I can't get it to display data in the UserControl YET, just the UserControl itself.

Resources