Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
315
XamComboEditor: ValueToDisplayTextConverter for Enums
posted

I would like to use a IValueEditor to customize the text in a XamComboBox. I tried ValueToDisplayTextConverter, but it didn't work as expected...

XAML:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:WpfApplication3"
    xmlns:Editors="clr-namespace:Infragistics.Windows.Editors;assembly=Infragistics3.Wpf.Editors.v9.1">
   
    <Control.Resources>
        <ObjectDataProvider MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}"
                            x:Key="_values">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="Visibility" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Control.Resources>
   
    <StackPanel>
        <Editors:XamComboEditor ItemsSource="{Binding Source={StaticResource _values}}" ValueType="{x:Type TypeName=Visibility}">
            <Editors:XamComboEditor.ValueToDisplayTextConverter>
                <local:VisibilityConverter />
            </Editors:XamComboEditor.ValueToDisplayTextConverter>
        </Editors:XamComboEditor>
    </StackPanel>
</Window>

 

Codebehind:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication3
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    [ValueConversion(typeof(Visibility), typeof(string))]
    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return string.Empty;
            }

            var visibility = (Visibility) value;
            switch (visibility)
            {
                case Visibility.Visible:
                    return "V";
                case Visibility.Hidden:
                    return "H";
                case Visibility.Collapsed:
                    return "C";
                default:
                    return "Unknown";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var text = value.ToString();
            switch (text)
            {
                case "V":
                    return Visibility.Visible;
                case "H":
                    return Visibility.Hidden;
                case "C":
                    return Visibility.Collapsed;
                default:
                    return null;
            }
        }
    }
}

 

What am I doing wrong?

Thanks,
Patrick

Parents
No Data
Reply
  • 54937
    Suggested Answer
    Offline posted

    The ValueToDisplayTextConverter is used to determine the text that is displayed in the edit area for a given value. I'm guessing that you were assuming this would affect the drop down portion but this is not how the converter is used. To affect the dropdown portion you would need to bind to a different data source and set the DisplayMemberPath and ValueMemberPath to the appropriate property names of the items. Another option would be to set the ComboBoxStyle property to a custom style for a ComboBox and set the ItemTemplate of that to a custom template where you can try to handle the conversion of the data item to a different representation.

Children
No Data