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
495
XamComboEditor Display texts
posted

Hello,

I have a unique situation, it can be quite confusing, so I'll to dish it out as clear as I can.

I have an entity, we'll call it "Item", and that entity has an Nullable<int> (in my sample I just use int for simplicity) property called "IntDetail".

IntDetail could be one of these values: 10,20,30,40 - but in some cases the user can also specify some other number (int, for example 70). A Classic control for this scenario would be just a NumericEditor, but the client was very specific about wanting a ComboBox here. In that case, this becomes an IsEditable ComboBox.

I want the following to happen:

1. When the ComboBox is not in edit mode, the text displayed should be "x details" (for example: 20 details).

2. When I open the ComboBox, all the items appear as their value alone. (10, 20, etc.). The display text on the TextBox part of the ComboBox can display either the full string or the value, doesn't matter.

3. After selecting, the TextBox doesn't have to show the full string. Showing the full string after exiting EditMode is fine.

 

This sounds rather simple (and is that simple in a regular ComboBox), yet I'm having serious trouble with it in XamComboBox.

 

Following is code I tried to create this situation with:

<Window
    x:Class="IG_Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:igEditors="http://infragistics.com/Editors"
    xmlns:local="clr-namespace:IG_Test"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    SizeToContent="WidthAndHeight"
    Title="Window1">
    <Window.Resources>
        <local:SimpleConverter x:Key="SimpleConverter"/>
    </Window.Resources>
    <Grid Margin="14">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="7"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock
            Grid.Column="0"
            Grid.Row="0"
            Text="Real Value"/>
        <TextBlock
            Grid.Column="2"
            Grid.Row="0"
            HorizontalAlignment="Center"
            Text="{Binding IntDetail}"/>
        <!--ItemsSource with ValueToDisplayTextConverter-->
        <TextBlock
            Grid.Column="0"
            Grid.Row="1"
            Text="ItemsSource with ValueToDisplayTextConverter"/>
        <igEditors:XamComboEditor
            Name="combo1"
            Width="150"
            Grid.Column="2"
            Grid.Row="1"
            SelectedItem="{Binding IntDetail}"
            ValueToDisplayTextConverter="{StaticResource SimpleConverter}"/>
        <!--ItemsSource with ValueToTextConverter-->
        <TextBlock
            Grid.Column="0"
            Grid.Row="2"
            Text="ItemsSource with ValueToTextConverter"/>
        <igEditors:XamComboEditor
            Name="combo2"
            Width="150"
            Grid.Column="2"
            Grid.Row="2"
            SelectedItem="{Binding IntDetail}"
            ValueToTextConverter="{StaticResource SimpleConverter}"/>
        <!--ItemsSource with ValueToTextConverter and IsEditable-->
        <TextBlock
            Grid.Column="0"
            Grid.Row="3"
            Text="ItemsSource with ValueToTextConverter and IsEditable"/>
        <igEditors:XamComboEditor
            Name="combo3"
            Width="150"
            Grid.Column="2"
            Grid.Row="3"
            IsEditable="True"
            SelectedItem="{Binding IntDetail}"
            ValueToTextConverter="{StaticResource SimpleConverter}"/>
        <!--ItemsProvider with ComboBoxDataItems-->
        <TextBlock
            Grid.Column="0"
            Grid.Row="4"
            Text="ItemsProvider with ComboBoxDataItems"/>
        <igEditors:XamComboEditor
            Name="combo4"
            Width="150"
            Grid.Column="2"
            Grid.Row="4"
            Value="{Binding IntDetail}">
            <igEditors:XamComboEditor.ItemsProvider>
                <igEditors:ComboBoxItemsProvider>
                    <igEditors:ComboBoxDataItem DisplayText="10 details" Value="10"/>
                    <igEditors:ComboBoxDataItem DisplayText="20 details" Value="20"/>
                    <igEditors:ComboBoxDataItem DisplayText="30 details" Value="30"/>
                    <igEditors:ComboBoxDataItem DisplayText="40 details" Value="40"/>
                </igEditors:ComboBoxItemsProvider>
            </igEditors:XamComboEditor.ItemsProvider>
            <igEditors:XamComboEditor.Resources>
                <DataTemplate DataType="{x:Type igEditors:ComboBoxDataItem}">
                    <TextBlock Text="{Binding Value}"/>
                </DataTemplate>
            </igEditors:XamComboEditor.Resources>
        </igEditors:XamComboEditor>
        <!--ItemsProvider with ComboBoxDataItems and IsEditable-->
        <TextBlock
            Grid.Column="0"
            Grid.Row="5"
            Text="ItemsProvider with ComboBoxDataItems and IsEditable"/>
        <igEditors:XamComboEditor
            Name="combo5"
            Width="150"
            Grid.Column="2"
            Grid.Row="5"
            IsEditable="True"
            Value="{Binding IntDetail}">
            <igEditors:XamComboEditor.ItemsProvider>
                <igEditors:ComboBoxItemsProvider>
                    <igEditors:ComboBoxDataItem DisplayText="10 details" Value="10"/>
                    <igEditors:ComboBoxDataItem DisplayText="20 details" Value="20"/>
                    <igEditors:ComboBoxDataItem DisplayText="30 details" Value="30"/>
                    <igEditors:ComboBoxDataItem DisplayText="40 details" Value="40"/>
                </igEditors:ComboBoxItemsProvider>
            </igEditors:XamComboEditor.ItemsProvider>
            <igEditors:XamComboEditor.Resources>
                <DataTemplate DataType="{x:Type igEditors:ComboBoxDataItem}">
                    <TextBlock Text="{Binding Value}"/>
                </DataTemplate>
            </igEditors:XamComboEditor.Resources>
        </igEditors:XamComboEditor>
    </Grid>
</Window>

 

using System.ComponentModel;
using System.Windows.Data;
using System;
using System.Collections.ObjectModel;
namespace IG_Test
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();

            ObservableCollection<int> col = new ObservableCollection<int> { 10, 20, 30, 40 };
            combo1.ItemsSource = col;
            combo2.ItemsSource = col;
            combo3.ItemsSource = col;

            DataContext = new Item { IntDetail = 20 };
        }
    }

    public class Item : INotifyPropertyChanged
    {
        private int _intdetail;
        public int IntDetail
        {
            get
            {
                return _intdetail;
            }
            set
            {
                _intdetail = value;
                RaisePropertyChanged("IntDetail");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class SimpleConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value == DBNull.Value)
                return "none selected";

            return System.Convert.ToInt32(value) + " deatils";
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
}

 

 

Comments:

1. ItemsSource with ValueToDisplayTextConverter - Doesn't work at all.

2. ItemsSource with ValueToTextConverter - Resets value when ComboBox is opened, If no value is then selected, it stays null.
Also shows "20 details" when selecting 20 and leaving focus, but does not do so when value is changed externally or when the window first loads. Also displays some red rectangle for a reason I didn't explore further.

3. ItemsSource with ValueToTextConverter and IsEditable - Same as above.

4. ItemsProvider with ComboBoxDataItems / ItemsProvider with ComboBoxDataItems and IsEditable - The best 'hack' I came up with. Works partially. Acts as expected when IsEditable=False, but does not display "70 details" when entering a new value.

 

Thank you for any help.