0
public enum Type
{
    One = 1,
    Two = 2,
    Three = 3
}

public void Method()
{
    Type type = Type.One;

    var binding = new Binding(type - ???);
    binding.Converter = ?????;

    var child = new FrameworkElementFactory(typeof(ComboBox));
    child.SetValue(ComboBox.ItemsSourceProperty, Enum.GetValues(typeof(Type)));
    child.SetValue(ComboBox.SelectedValueProperty, binding);
}

I want to bind type to XAML in the code. But I don't know how I can right to do it. And I don't know the converter (Enum<->String) name. I only know it's the standard converter.

Questions:

  1. How can I right to do it?
  2. Where can I find the list of all standard converters?
Pro
  • 673
  • 2
  • 6
  • 15
  • Why do it in code? It's easier from XAML https://stackoverflow.com/questions/14279602/how-can-i-use-enum-types-in-xaml – Richardissimo Jul 21 '18 at 05:56
  • @Richardissimo I must do it in the code it's a task. How can I do in XAML - I know. But how can I do it in the code... I nothing found on the Internet. :( – Pro Jul 21 '18 at 06:01

1 Answers1

0

Set the Source of the binding to the object to which you want to bind. If the object implements INotifyPropertyChanged then use Path to listen to a particular property, and raise the property changed event when it is set.

I'm not sure there is a standard enum to string converter, although you can use StringFormat on the binding or implement a custom converter. There is some automatic type conversion based on the DependencyProperty you're binding this to so you may not even need it.

You can find a list of "standard"" converters here as derived types of the interface. There aren't many. https://learn.microsoft.com/en-gb/dotnet/api/system.windows.data.ivalueconverter?view=netframework-4.7.2

You also need to use SetBinding to actually set the binding on the DependencyProperty of the DependencyObject. See https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-create-a-binding-in-code

George Helyar
  • 4,319
  • 1
  • 22
  • 20