Get databinding object in control - c#

I want to create an enumcombobox where the popup will show the enumvalues of the controls' binding object. Somehow I cannot get the binding object property at runtime. Databindings wil get me to the binding object. But the property and its type is invisable for me, or I just didn't find it yet... Can anyone help me with this?

You have to use a DataObjectProvider. In your resources, put something like :
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odpEnum">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="yourEnumNameHere"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
In your combo, put :
<ComboBox ItemsSource="{Binding Source={StaticResource odpEnum}}"/>
This should fill your combo with your enum.

Related

mscorlib in .net core is missing in v3.1

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>

WPF Combobox Itemssource is Visibility Enum

So i have a simple question; I want to display the System.Windows.Visibility Enum in a ComboBox. However i don't seem to find the path to it. Searched like a crazy man but couldn't find anyone who know the path to this enum.
I'm aware that this can be done in code (and already have it working) but i prefer to do it in XAML.
Could anyone help me out?
Voila:
<ComboBox SelectedIndex="0">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
<Visibility>Hidden</Visibility>
</ComboBox>
No additional namespaces needed.
#Maciek's solution is the simplest and quickest to apply, however more general, pure XAML way of listing enum values is this:
<FrameworkElement.Resources>
<ObjectDataProvider x:Key="ItemsSource"
ObjectType="{x:Type sys:Enum}"
MethodName="GetValues"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Visibility" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</FrameworkElement.Resources>
(...)
<ComboBox ItemsSource="{Binding Source={StaticResource ItemsSource}}" />
You can then simply replace Visibility with any other enum type to get a list of its values.
If you want to bind enum values to ItemsSource, you need a property in ViewModel with enum values. Than you have to bind ItemsSource to this property. You get values of enum like this:
Enum.GetValues(typeof(EffectStyle)).Cast<EffectStyle>();
If you can't find a path to your property, that means you have wrong DataContext. Set it to your ViewModel class or to your window, if your are not using MVVM. In the simplest scenario, you could set DataContext to your window in Window constructor.
this.DataContext = this;
Take a look at this or this link, as a big picture of DataContext and Binding in WPF.
If you are interested, also read about MVVM in WPF. You could use MVVM Light
If you want a fully xaml solution for your enum binding to ComboBox, you could write sth like this or use ObjectDataProvider like here.
I think Maciek's solution is good enough in your case.
If you want to do it using code, let's say your combobx has an x:name = my combo you can simply do:
mycombo.ItemsSource = Enum.GetValues(typeof(Visibility)).Cast<Visibility>();

Unable to show BindingMode Enum in comboBox (WPF)

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"

ComboBox for enumeration System.IO.Ports.Parity in WPF

I want (in C#) to populate the list of admissible values for a combobox with the admissible values of the enumeration System.IO.Ports.Parity. To this end I created a collection:
public class theParitySource : ObservableCollection<Parity>
{
public theParitySource()
{
Array parities = System.Enum.GetValues( typeof( Parity ) );
foreach (Parity p in parities) this.Add(p);
}
}
(btw: is there oneliner for this initialization?) and made this the datacontext of the combobox:
...
xmlns:local="clr-namespace:myNamespace"
...
<ComboBox ...>
<ComboBox.DataContext>
<local:theParitySource />
</ComboBox.DataContext>
</ComboBox>
The combobox, however, remains empty (it is shown as empty, but seems to have the correct length), even though I can see in the Debugger how theParitySource gets populated. This approach does work in another combobox (even in the same class) where I do this for a baudrate. That I initialize with integer values, so I guess it is somehow related to the fact that I'm using an enum here, but I'm clueluess what might be the reason. Any pointers? Do I need to write a converter?
(Of course I can work around this by creating a list of strings from the enum, but this would be kind of unpleasant...)
Edit: actually I'd prefer to do all of this in XAML. Is there a simple way to do that?
You can do this all in the Xaml using ObjectDataProvider
In your Window.Resources (or whatever resources you are using) setup a ObjectDataProvider.
To setup the ObjectDataProvider for Enums you set the ObjectType to {x:Type sys:Enum} and the MethodName to GetValues to fill the ComboBox with the actual Enums or you can use GetNames to fill the ComboBox with a string representaion of the Enum
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:io="clr-namespace:System.IO.Ports;assembly=System"
<Window.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="ParityValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="io:Parity" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
Then bind to your ComboBox
<ComboBox ItemsSource="{Binding Source={StaticResource ParityValues}}" />
Result:
You need to bind to a proper path, you might be setting its data context, but you're not telling it with property to display.
You need to tell it what part of the bound context you are wanting to display and which you are wanting to be 'selected'.
WPF Combobox DisplayMemberPath

Designer throws exception when I use ObjectDataProvider and set Type property

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" />

Categories

Resources