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 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.
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.
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.
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!
Hi,
Thanks very much. That is nice to hear.
Please let me know if there is anything else I can help with.
Thanks,
i made an exactly identical example yesterday afternoon (or last night in your time zone) ;)
Nice to see such great and fast help in this forum!
Sincerely,Jochen
I'm going to add an updated version of your sample to reflect the revisions suggested by you and Neelima.
Thanks for your input to the forums. I'm sure it will be helpful to others.
hi Marianne,
thanks for the example but this was not what i looked for.
As you wrote, i expected to see other than the DisplayText after an item in the xamComboEditor had been selected!
Neelima had the right answer. I had to change the ItemTemplate of the ComboBoxStyle of the Editor. This worked fine!
Thanks anyway,
Jochen
Looks like you may already have found a solution for yourself. I was working with your sample and in case it is of any help I’ll add it back into the thread.
If you question related to binding to the values associated with the selecteditem of the xamComboEditor, you’ll see that I’ve modified your sample adding additional textboxes and binding each textbox to a different property of your comboBox’s SelectedItem.
I also commented out the SelectedItemChanged code since that isn’t necessary to update the value of the textboxes.
I did need to use a OneWay Mode for binding the DisplayText to the textbox but that is because DisplayText only has a Getter.
It wasn’t clear to me if you are expecting to see other than the DisplayText after an item in the xamComboEditor had been selected. Or how your question relates to the XamComboEditor being in a xamDataPresenter. Please give me more information if I haven’t addressed your question.
Please take a look and let me know if I can be of further help.