I have a class BatchInfoViewModel that contains an enum:
namespace MyStuff.ViewModel
{
public class BatchInfoViewModel : ObservableObject
{
public enum TimeFrame
{
Today,
Last7days,
Last30days,
Last6months,
Last12months,
All
}
}
}
and a user control 'BatchInfoView' that uses a BatchInfoViewModel and I'm trying to bind a combobox in this view, to the TimeFrame enum on the model, but every resource I've found shows what I think is the method I'm using, but I keep getting Type not found exceptions when running.
<UserControl x:Class="MyStuff.View.BatchInfoView"
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:view="clr-namespace:MyStuff.View"
xmlns:viewModel="clr-namespace:MyStuff.ViewModel;assembly=MyStuff.ViewModel"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
<ObjectDataProvider x:Key="EnumDataProvider"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<!--None of these work at all, I'm lost :( I've tried variations of these: -->
<!--<viewModel:BatchInfoViewModel></viewModel:BatchInfoViewModel>
<x:Type TypeName="viewModel:TimeFrame"/>
<x:Type TypeName="BatchInfoViewModel:TimeFrame"/>-->
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
It can't find the Types and will throw an exception.
You have an enum nested in a class, place the enum in the namespace, outside of any class and use viewModel:TimeFrame.
(I tested the + concatenation syntax which you can use for x:Static on enums but it does not appear to apply here)
Related
I am working on wpf app. Using ObjectDataProvider am trying to bind enum to combobox. But mscorlib is not appearing when trying to reference in XAML. Does any package need to be installed for this?
There is no reason to reference mscorlib, just map the namespace to System.Runtime:
<ObjectDataProvider x:Key="name"
xmlns:core="clr-namespace:System;assembly=System.Runtime"
MethodName="GetValues"
ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:YourType"></x:Type>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
On digging more on how to bind enums in .net core, found an another way of achieving the same as described below.
The default namespace in XAML window is
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
I observered that, even without referring the 'system.runtime' assembly in XAML. we can still parse an enum and bind in XAML. Here is the code that is tried and successfully works.
Enum
public enum Members
{
Member1,
Member2
}
XAML
<ObjectDataProvider ObjectType="local:Members"
x:Key="key"
MethodName="GetValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Members"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
I have a project along with main project in my solution. I want to localize the content in this xaml file in this project.
I use the ways that have been discussed here How to change UI language using resource dictionary at run time in MVVM?.
However I could not find ObjectDataProvider in any way.
<UserControl xmlns:languageHelper="clr-namespace:XX"
<UserControl.Resources>
<ObjectDataProvider x:Key="Resources" ObjectType="{x:Type languageHelper:CultureResources}" MethodName="GetResourceInstance"/>
</UserControl.Resources>
</UserControl>
and I use this code to find ObjectDataProvider but i coulnt get it through
public static ObjectDataProvider ResourceProvider
{
get
{
if (m_provider == null)
m_provider = (ObjectDataProvider)System.Windows.Application.Current.FindResource("Resources");
return m_provider;
}
}
Resources.Culture = culture;
ResourceProvider.Refresh();
It shows System.Windows.ResourceReferenceKeyNotFoundException: ''Resources' resource not found.'
Well the problem is that you are creating your ObjectDataProvider in your UserControls resources. I think you have to create that in your App.xaml file
here is an example:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ObjectDataProvider x:Key="Resources"
ObjectType="{x:Type languageHelper:CultureResources}"
MethodName="GetResourceInstance"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have my ResourceDictionary declared as:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AutoStart.View"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=mscorlib">
<ObjectDataProvider x:Key="processPrioritiesList" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="diagnostics:ProcessPriorityClass"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</ResourceDictionary>
And it throws an error saying:
The name "ProcessPriorityClass" does not exist in the namespace
"clr-namespace:System.Diagnostics;assembly=mscorlib".
I checked that ProcessPriorityClass is a public member of the System.Diagnostics namespace, but for some reason it's not letting me use the Enum value to be binded to a combobox
How could i accomplish this?
Wrong assembly name:
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=System"
All, I have a user control. In the XAML markup for this control I want to define a resource (instatiate an object called cellColorConverter of the class CellColorConverter which is defined in the same namespace as the control. I have
<UserControl x:Class="ResourceStudio.Resource.Resource"
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" Height="331.2" Width="340">
<UserControl.Resources>
<ResourceStudio.Resource:CellColorConverter x:Key ="cellColorConverter"/> // <- Error.
</UserControl.Resources>
This is giving a compile-time error saying
The namespace prefix ResourceStudio.Res is not defined.
What am I doing wrong here?
Thanks for your time.
Update: I now have
xmlns:local="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio"
mc:Ignorable="d" Height="331.2" Width="340">
<UserControl.Resources>
<local:CellColorConverter x:Key ="cellColorConverter"/>
</UserControl.Resources>
The CellColorConverter class is in the name space ResourceStudio.Resource, defined as
namespace ResourceStudio.Resource
{
public class CellColorConverter : IMultiValueConverter
{
// ...
}
}
I still get the following error
The name "CellColorConverter" does not exist in the namespace "clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio". F:\Camus\ResourceStudio\ResourceStudio\ResourceStudio\Resource\Resource.xaml
In the XAML:
<ResourceStudio.Resource:CellColorConverter />
The ResourceStudio.Resource is an XML namespace for CellColorConverter.
You need to map this XML namespace to a .NET namespace:
<UserControl xmlns:ResourceStudio.Resource="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio" ... />
The actual namespace and assembly name depend upon what you've called them in your code.
This article provides more information.
Also, you can make the namespace shorter:
xmlns:local="clr-namespace:..."
<local:CellColorConverter ... >
When I try to use ObjectDataProvider and set type using Type property:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:eng="clr-namespace:ViKing.Engine;assembly=ViKing.Engine"
...
<ObjectDataProvider x:Key="proxyTypes"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="{x:Type eng:ProxyTypes}" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
WPF designer refuses to load with following exception:
Value cannot be null.
Parameter name: typeName
It looks like designer expects TypeName property to be set. But I don't know how to correctly set it. I tried to use full type name ViKing.Engine.ProxyTypes but no luck there.
Ok I got it. Type tag should look like this:
<x:Type TypeName="eng:ProxyTypes" />