Hello,
I create a xamDataPresenter as Grid with several Combobox Columns. But the Label of the closed ComboEditior should show the text of a different Property than the DisplayText Property. I tried to make a simple sample with a single xamComboEditor, but i didn't get it running as i want. (the sample is attached in this post)
What i did:I created a Class for a ComboItem with three Properties (DisplayText, LabelText and Value).I added a List of objects of that Class to the ItemsSource of a ComboBoxItemsProvider.I set the ValuePath-Property to "Value" and the DisplayMemberPath-Property to "DisplayText". Then i bind the Itemsprovider to the xamComboEditor. Everything works fine until that. I tried to find a Property in the xamComboEditor to set the LabelText. The only property which made sence to me was the Text-PRoperty of the editor but this doesnt work because when you set this Property, the Editor tries to find a new fittable Item in its itemssource which leads to a exception. I also tried this: this.xamComboEditor1.DisplayValueSource = DisplayValueSource.Value; but it leads also to an exception.
Here's the code:
using System.Collections.Generic; using System.Windows; using System.Windows.Documents; using Infragistics.Windows.Editors; namespace WpfApplication1 { /// <summary> /// Interaktionslogik für MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<DhpComboItem> itemList = new List<DhpComboItem>() { new DhpComboItem(1, "one"), new DhpComboItem(2, "two"), new DhpComboItem("drei", "three"), new DhpComboItem(4, "four") }; ComboBoxItemsProvider provider = new ComboBoxItemsProvider(); provider.ItemsSource = itemList; provider.ValuePath = "Value"; provider.DisplayMemberPath = "DisplayText"; this.xamComboEditor1.ValueType = typeof(object); this.xamComboEditor1.ItemsProvider = provider; //this.xamComboEditor1.DisplayValueSource = DisplayValueSource.Value; this.xamComboEditor1.SelectedIndex = 0; } private void xamComboEditor1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { this.xamComboEditor1.Text = ((DhpComboItem)this.xamComboEditor1.SelectedItem).LabelText; } } public class DhpComboItem { public DhpComboItem(object value, string labelText) { Value = value; LabelText = labelText; } public object Value { get; set; } public string LabelText { get; set; } public string DisplayText { get { string displayText = string.Empty; if (Value != null) displayText = Value.ToString() + "\t"; return displayText + LabelText; } } } }Here's the xaml code:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igEditors="http://infragistics.com/Editors" xmlns:igDP="http://infragistics.com/DataPresenter" Title="MainWindow" Height="350" Width="525"> <Grid> <igEditors:XamComboEditor HorizontalAlignment="Left" Margin="184,196,0,0" Name="xamComboEditor1" VerticalAlignment="Top" Width="228" SelectedItemChanged="xamComboEditor1_SelectedItemChanged" /> <TextBox Text="{Binding ElementName=xamComboEditor1, Path=SelectedItem.Value}" Height="23" HorizontalAlignment="Left" Margin="184,236,0,0" Name="textBox1" VerticalAlignment="Top" Width="228" /> </Grid> </Window>
Thanks for your help,Jochen
This works perfect!
I only changed this:
<StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Value}"/><TextBlock Text="--"/><TextBlock Text="{Binding LabelText}"/></StackPanel>
to this:
<TextBlock Text="{Binding DisplayText}"/> because my ComboItem Class already has a property which does that job.
and i forced the ComboEditor to end the editmode when the dropdown is closed to change the text in the comboeditor immediatly.
Thank you very much!
I think I got your question now.
Try this:
1 - Implement ValueToDisplayConverter on XamComboEditor
public class DisplayTextConverter : IValueConverter {
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var dhcpItem = value as DhpComboItem; if (dhcpItem == null) return null;
return dhcpItem.LabelText; }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; }
#endregion }
2 - Implement ComboBoxStyle with Item Template
<Grid><Grid.Resources><WpfApplication1:DisplayTextConverter x:Key="DisplayTextConverter"/></Grid.Resources><igEditors:XamComboEditor HorizontalAlignment="Left" Margin="184,196,0,0" Name="xamComboEditor1" VerticalAlignment="Top" Width="228"ValueToDisplayTextConverter="{StaticResource DisplayTextConverter}"ValuePath="Value" DisplayMemberPath="DisplayText"><igEditors:XamComboEditor.ComboBoxStyle><Style TargetType="ComboBox"><Setter Property="ItemTemplate"><Setter.Value><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Value}"/><TextBlock Text="--"/><TextBlock Text="{Binding LabelText}"/></StackPanel></DataTemplate></Setter.Value></Setter></Style></igEditors:XamComboEditor.ComboBoxStyle></igEditors:XamComboEditor><TextBox Text="{Binding ElementName=xamComboEditor1, Path=SelectedItem.LabelText}" Height="23" HorizontalAlignment="Left" Margin="184,236,0,0" Name="textBox1" VerticalAlignment="Top" Width="228" /></Grid>
3 - Remove .ValuePath and .DisplayMemberPath code in code behind file.
InitializeComponent();
List<DhpComboItem> itemList = new List<DhpComboItem>() { new DhpComboItem(1, "one"), new DhpComboItem(2, "two"), new DhpComboItem(3, "three"), new DhpComboItem(4, "four") };
ComboBoxItemsProvider provider = new ComboBoxItemsProvider(); provider.ItemsSource = itemList; //provider.ValuePath = "Value"; //provider.DisplayMemberPath = "DisplayText";
this.xamComboEditor1.ValueType = typeof(object); this.xamComboEditor1.ItemsProvider = provider; this.xamComboEditor1.SelectedIndex = 0;
And this works. Let me know if this doesn't work for you.
Thanks for your help but that's not the point.I know this throws an exeption.I already wrote:"I tried to find a Property in the xamComboEditor to set the LabelText. The only property which made sence to me was the Text-PRoperty of the editor but this doesnt work because when you set this Property, the Editor tries to find a new fittable Item in its itemssource which leads to a exception."This was only a try!Perhaps i did not make clear what i need:If the DropDown Menu is open, then the "DisplayText"-Property of every Item should be shown (like it actually does in the example!)BUT if you select an item, and the ComboEditor is CLOSED, the "LabelText"-Property of the selected Item should be shown.
This code is the culprit and is redundant:
private void xamComboEditor1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { this.xamComboEditor1.Text = ((DhpComboItem)this.xamComboEditor1.SelectedItem).LabelText; }
At some point SelectedItem is null and throwing an exception. You don't need this code if your intent is to display the text of the item selected. You have set the DisplayMemberPath = "DisplayText" that should take care of it.
Solution: Remove SelectedItemChanged code and it works.