none
Binding Type variable RRS feed

  • Вопрос

  • Is it possible to bind the value of a type variable using wpf and xaml. I am trying to bind a enum type.

    public Type EnumComboValues { get; set; } = typeof(LoadingPhases);
    <View:SelectBoxComponent x:Name="ModuleType_SB" HelperText="Module Type" EnumComboValues="HOW DO I BIND THIS"/>


    15 декабря 2019 г. 0:28

Все ответы

  • Hi,
    try this demo:

    XAML:

    <Window x:Class="WpfApp1.Window79"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            mc:Ignorable="d"
            Title="Window79" Height="450" Width="800">
      <Window.Resources>
        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValues">
          <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Window79EnumComboValues" />
          </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
      </Window.Resources>
      <StackPanel>
        <ComboBox ItemsSource="{Binding Source={StaticResource EnumValues}}" />
      </StackPanel>
    </Window>

    And the enum:

    using System.Windows;
    
    namespace WpfApp1
    {
      public enum Window79EnumComboValues
      {
        Fierst,
        Second,
        Third
      }
    }
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    15 декабря 2019 г. 19:37
  • Is there a simple way to bind any enum type to a 'type' variable. Not so much specifically putting the values in a combo box.

    I have seen something like this seen this before.

    TypeVaribleName="{x:Type namespace.NameSpaceOfEnum NameOfEnumClass}"
    However, I get an error which states that nested types are not supported.

      
    16 декабря 2019 г. 0:45
  • Hi,

    If you want to bind Enum, without the simple way you said, but you can also try the following methods:

     [MarkupExtensionReturnType(typeof(IEnumerable))]
    
        public class EnumValuesExtension : MarkupExtension
    
        {
    
            public EnumValuesExtension()
    
            {
    
            }
    
            public EnumValuesExtension(Type enumType)
    
            {
    
                this.EnumType = enumType;
    
            }
    
            [ConstructorArgument("enumType")]
    
            public Type EnumType { get; set; }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
    
            {
    
                if (this.EnumType == null)
    
                    throw new ArgumentException("The enum type is not set");
    
                return Enum.GetValues(this.EnumType);
    
            }
    
        }
     <ComboBox  ItemsSource="{Binding Source={local:EnumValuesExtension {x:Type local:Window79EnumComboValues}}}"/>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    16 декабря 2019 г. 5:46
    Модератор