Binding an enumerated type into a text field

I bind the value of textbox.text to an enumeration type. My enum looks like this:

public enum Type
    {

        Active,

        Selected,

        ActiveAndSelected
    }

What I did not want was to show in the text field "Active mode" instead of "Active", etc. Can this be done? It would be great if I could implement this in XAML - because I have all the bindings in the style.xaml style file

I tried to use description attributes, but it seems that this is not enough

+3
source share
3 answers

IMHO, using a converter is the best approach.

, , , . ( ):

    public enum StepStatus {
    [StringValue("Not done yet")]
    NotDone,
    [StringValue("In progress")]
    InProgress,
    [StringValue("Failed")]
    Failed,
    [StringValue("Succeeded")]
    Succeeded
}

, , StringValue . Google "String Enumerations in # - CodeProject", CodeProject (, ..)

, :

    [ValueConversion(typeof(StepStatus), typeof(String))]
public class StepStatusToStringConverter: IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
        String retVal = String.Empty;

        if (value != null && value is StepStatus) {
            retVal = StringEnum.GetStringValue((StepStatus)value);
        }

        return retVal;
    }

    /// <summary>
    /// ConvertBack value from binding back to source object. This isn't supported.
    /// </summary>
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture) {
        throw new Exception("Can't convert back");
    }
}

, XAML:

<resourceWizardConverters:StepStatusToStringConverter x:Key="stepStatusToStringConverter" />
...
<TextBox Text="{Binding Path=ResourceCreationRequest.ResourceCreationResults.ResourceCreation, Converter={StaticResource stepStatusToStringConverter}}" ... />

; , , .

+9

. Stringformat. '{}' - escape-, , . ( '{0}'), .

<Window x:Class="TextBoxBoundToEnumSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Text="{Binding ModeEnum,StringFormat={}{0} Mode}"/>
        <Button Click="Button_Click" Height=" 50">
            Change to 'Selected'
        </Button>
    </StackPanel>
</Window>

using System.ComponentModel;
using System.Windows;

namespace TextBoxBoundToEnumSpike
{

    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private ModeEnum m_modeEnum;
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
            ModeEnum = ModeEnum.ActiveAndSelected;
        }

        public ModeEnum ModeEnum
        {
            set
            {
                m_modeEnum = value;
                if (PropertyChanged!=null)PropertyChanged(this,new PropertyChangedEventArgs("ModeEnum"));
            }
            get { return m_modeEnum; }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ModeEnum = ModeEnum.Selected;
        }
    }

    public  enum ModeEnum
    {
        Active,
        Selected,
        ActiveAndSelected
    }
}
+5

You can use the Converter for this. Normally bind to an enumeration, but add a converter property to the binding. A converter is a class that implements IValueConverter that WPF will call. There you can add a suffix like "Mode" (or do whatever you like).

+1
source

Source: https://habr.com/ru/post/1764391/


All Articles