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"
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'm trying to show the original BindingMode enum in a comboBox (the list of TwoWay`OneWay` etc.).
In the XAML file, inside Resources I need to set ObjectDataProvider to be the type of BindingMode:
<!--BindingMode combo box-->
<ObjectDataProvider x:Key="BindingMode" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="sys:BindingMode"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
The problem is that I can't include System.Windows.Data in 'xmlns', i've tried to declare:
xmlns:sysWinData="clr-namespace:System.Windows.Data;assembly=mscorlib"
but it won't work. What am I missng here? Thanks.
System.Windows.Data resides in PresentationFramework.dll so change your declaration to
xmlns:sysWinData="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
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)
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" />