Namespace path in WPF XAML - c#

I have Content directory in main solution.
In content catalog i have two catalogs: ViewModels and Views
In XAML, i have declared:
xmlns:vm ="clr-namespace:AppName.Content"
now, i want get reference to some class in ViewModel catalog:
<DataTemplate DataType="{x:Type vm:LaserPathViewModel}">
I know thats wrong because the namespace of LaserPathViewModel is AppName.Content.ViewModels.
But how get this reference without add next one namespace declaration?

You don't. You have to declare that other namespace. One way to do that is by adding another namespace declaration:
xmlns:vm2 ="clr-namespace:AppName.Content.ViewModel"
And then you can use it like this:
<DataTemplate DataType="{x:Type vm2:LaserPathViewModel}">
But there is another way to declare the namespaces. You can use the XmlnsAttribute which allows you to map multiple .NET namespaces to one X(A)ML namespace. You can find some nice explanation here.

Related

Internal Members of Friend Assembly Not Accessible in XAML

Problem Statement
I have two assemblies, one with primarily data contracts and other containing the UI layer.
The data contracts are public and are used in DataTemplate tags in the XAML in UI assembly.
<DataTemplate DataType="{x:Type contracts:MyContract}">
///other stuff
</DataTemplate>
The data contract is assembly is referred like this:
<xmlns:contracts="clr-namespace:Contracts.DataContracts,assembly=Contracts.DataContracts"
Now I would like to make everything internal DataContract assembly to keep low API surface. I have added UI assembly as friend assembly in the AssemblyInfo class of DataContracts assembly. However, I get the following error in the XAML when I compile the UI assembly:
Only public or internal classes can be used within markup. 'MyContract' type is not public or internal.
I have checked that the InternalVisibleTo is working as I can access other internal stuff in code behind. But in XAML it is not possible.
What are the possible ways to get around this?
Not too clear on what stuff you are trying to extend from an assembly into your application, but when I have XAML in a library, I tend to reference it this way in an application.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/PathInAssemblyIfNeeded/YourXAMLReferences.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
If this does not help, give more information on what you are a trying to accomplish.

Cant figure out how to set up up a static resource

I have a grid that I am trying to hook up a visibility converter to. I have a class set up in the HelperObjects namespace, but for some reason the xaml is not picking up the reference. The error message is "The resource BoolToVisConverter could not be resolved" Why won't the xaml pull in this resource? I am sure I am doing something ridiculous here...
Here is the xaml:
xmlns:HelperObjects="clr-namespace:foo.HelperObjects"
...
<Grid Visibility="{Binding isZoneTwoVisible, Converter={StaticResource BoolToVisConverter}}">
The error message is indicating that the resource identified by the key BoolToVisConverter, to which you're trying to bind, cannot be found.
The most likely explanation is that you haven't declared that resource within a scope that can be accessed by your xaml. You'll want to create a StaticResource with a key matching the name you're referencing, within a Resources section of your xaml, the exact location may vary depending on your needs/structure.
Assuming you're doing this within a Window, you could do something like:
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Window.Resources>
<Grid Visibility="{Binding isZoneTwoVisible, Converter={StaticResource BoolToVis}}">
<--...Content...-->
</Grid>
</Window>
Note: I haven't included your namespace in front of the BooleanToVisibilityConverter because this is a class which already exists within the framework.
It may be the case you require slightly different behaviour, or don't have access to that class, in which case you may need to add your namespace when defining the resource, e.g. <HelperObjects:BooleanToVisibilityConverter x:Key="BoolToVis"/>
Potentially useful further info about static resources: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/staticresource-markup-extension
Try to specify the converter in UserControlResources or even parent level Grid Resources. Assuming you are using a UserControl.
And your code is unclear as of where you are defining the StaticResource BoolToVisConverter. But generally heres the process.
Look at this sample code below to define your StaticResorce in the UserControl Resources section. Make sure you set the DataContext where isZoneTwoVisible is residing. I am hoping you implemented ImplementINotifyPropertyChanged and said PropertyChanged on your isZoneTwoVisible or isZoneTwoVisible is a DependencyProperty. Note: BooleanToVisibilityConverter is a class that implements an IValueConverter or if your property is a bool you don't even need that class.
<UserControl.Resources>
<BooleanToVisibilityConverter
x:Key="boolToVisibility"></BooleanToVisibilityConverter>
</UserControl.Resources>
You did all the things said above and it still dosen't work, sometimes I specify the relative Source hoping its residing in a UserControl.
Visibility="{Binding isZoneTwoVisible,Converter={StaticResource boolToVisibility},RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}">

What is the prefix Fluent signify in the WPF control name "Fluent:RibbonWindow"?

In FluentRibbon project, the XAML file contains:
<Fluent:RibbonWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
I know that RibbonWindow is a class (derived from WPF Window class). It's the Fluent: prefix that I don't understand.
The same type of construct is also used by MahMetro like so:
<Metro:MetroWindow x:Class="FluentTest.MahMetroWindow"
What does the prefix signify in the above cases?
That's the namespace those controls/types live in. Some namespaces are included automatically when Xaml is converted to C#, but anything new that you bring in (either from 3rd party libraries or your own application) has to be explicit.
<UserControl xmlns:customControls="using:MyNamespace.CustomControls"
... more declarations ...
<customControls:MySpecialPanel />
</UserControl>

How do you embed a template as content in XAML?

Is there any way to declare a top-level window as a content template in xaml and recreate it at load time? Essentially I'm trying to do something equivalent to data templating:
<DataTemplate DataType="{x:Type my:MyType}" >
<Window>
...etc...
</Window>
</DataTemplate>
This works fine at run-time but it generates an error at compile time due to the fact that you're not supposed to use a top-level window to a data template or style etc, so basically I need to replace DataTemplate with a custom class to get around this. I don't actually need support for DataType...data templates set the resource key implicitly to DataType but a bug in WPF means you can't use the relevant attribute on your own types. I can easily get around that by doing things in reverse i.e. specifying the type as the key:
<MyClass x:Key="{x:Type my:MyType}" >
<Window>
...etc...
</Window>
</MyClass>
What I need to know though is how to declare MyClass so that the XAML content generates a template that I can create instances from rather than an actual instance of the Window itself.

Problems with adding EnumMatchToBooleanConverter to my xaml file

I am trying to follow this radiobutton tutorial
I created a class called EnumMatchToBooleanConverter and it is in the top level of my wpf project. It says to place the inside a window.resources like this:
<Window.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</Window.Resources>
I am using it in a usercontrol so I have placed it inside a stackpanel instead:
<StackPanel.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</StackPanel.Resources>
I have Microsoft Visual Studio Ultimate 2012 and it gives me an error:
EnumMatchToBooleanConverter is not supported in a Windows Presentation Foundation (WPF) project.
Any ideas as to what I am doing wrong? Am I not allowed to place it inside a stackpanel.resources?
I just tried placing it inside a grid.resources
<Grid.Resources>
<EnumMatchToBooleanConverter x:Key="enumConverter" />
</Grid.Resources>
and it says
The type 'EnumMatchToBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Well it is in the same namespace as the rest of my project, so I'm unsure why it isn't finding it.
Change
<EnumMatchToBooleanConverter x:Key="enumConverter" />
for
<local:EnumMatchToBooleanConverter x:Key="enumConverter" />
All non-built-in classes you reference in XAML must be prefixed by their corresponding xmlns prefix.
HighCore's got it right. Just to add to this namespace discussion, I thought I'd point out another approach that can help make the code more readable or help you diagnose where certain Controls/Value Converters/etc are coming from (i.e., which assembly they are really coming from). This technique could allow your XAML to appear like Christian has it in his blog (without the xmlns prefix):
<EnumMatchToBooleanConverter x:Key="enumConverter" />
Essentially you perform some some namespace mappings to consolidate namespaces like this (only works if the files are in a different assembly/project). So in my example above you have mapped one of your namespaces to the default xmlns, so you would not need any prefix in the XAML.
I'm still trying to figure out how far to take this technique and Paul Stovell talks about taking it to the extreme, like I've shown above.
Even if you don't end up applying it to that degree, knowing about it might come in handy if you're looking at someone else's Xaml and they have applied a mapping like that. Knowing that would remind you to lookup the AssemblyInfo.cs file and check for the mapping and possibly help you track down where a Control/Value Converter/etc is actually located.

Categories

Resources