WPF Combobox display entity name in dropdown instead of DisplayMemberPath - wpf

I have the following combobox:
<ComboBox Name="cbBonusType"
DisplayMemberPath="BonusTypeName"
SelectedValuePath="ID" Width="150" Margin="10,0,0,0"
SelectionChanged="cbBonusType_SelectionChanged"/>
When running:
When selecting an item, the combobox shows exactly the right string.
But while the droppbox is open, the named displayed in the droppbox are all set to the name of the entity: "CaSaMa.WPF.UI.Competiotion.BonusType".
Why is that and how do I fix it?

Best bet would be override the ToString on your object - that would give you a consistent display across your application.
hope that helps!
Just re-read your question - think I got the wrong end of the stick.. What you can do is set the ItemTemplate on the control instead like this;
<ComboBox Name="cbBonusType"
ItemTemplate="{StaticResource DisplayTemplate}"
SelectedValuePath="ID" Width="150" Margin="10,0,0,0"
SelectionChanged="cbBonusType_SelectionChanged"/>
then create the template like this
<DataTemplate x:Key="DisplayTemplate"
DataType="{x:Type <YOURTYPE>}">
<TextBlock Text="{Binding BonusTypeName}"/>
</DataTemplate>
its a known problem and this is the workaround sadly!
hope it helps.. this time! :)
edit : updated code just incase anyone else would like to use it!
ste.

Its way easier than this...in XAML:
<ComboBoxItem Content="This Value" Tag="This Value"/>
Then in code behind:
GetValue=ComboBoxName.selecteditem.tag.tostring()
GetValue will be "This Value" instead of
"System.Windows.Controls.ComboBoxItem: This Value"

Related

Xaml - combo box - why does selectedValue NOT work with multiple columns?

Hi and thanks for your help.
The following xaml works just fine:
<ComboBox Name="cboCit_type"
IsSynchronizedWithCurrentItem="True"
mvvm:View.FlowsWithPrevious="True"
ItemsSource="{Binding Path=cuCodeInfo.CitTypes}"
SelectedValuePath="code"
DisplayMemberPath="code"
Text="{Binding cit_type}"
IsEditable="true"
IsReadOnly="false"
SelectedValue="{Binding Path=cit_type}">
</ComboBox>
cuCodeInfo.CitTypes is simply a list of items that are available. There are a number of public properties, but the 2 in question are "code" and "description".
Right now, I show the available code values and the user selects one. If one was already selected, then it shows when the page is displayed. This is all good.
So then I thought it might be nice to show both the code and the description. I figured it shouldn't be too hard...
So I removed the DisplayMemberPath statement and added in an ItemTemplate.
When I did so everything looked great until I tried to select an item from the list. When I did so, instead of showing the selected code, I would get an empty string. I have searched the internet trying to find the one thing I need to add to the DataTemplate to fix this, but everything I have tried has failed. Here is the code that is NOT working:
<ComboBox Name="cboCit_type"
IsSynchronizedWithCurrentItem="True"
mvvm:View.FlowsWithPrevious="True"
ItemsSource="{Binding Path=cuCodeInfo.CitationTypes}"
SelectedValuePath="code"
Text="{Binding cit_type}"
IsEditable="true"
IsReadOnly="false"
SelectedValue="{Binding cit_type}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Border BorderThickness="0,0,1,0" BorderBrush="Black">
<TextBlock Text="{Binding Path=code}" mvvm:View.WidthEx="2" ></TextBlock>
</Border>
<TextBlock Text="{Binding Path=description}" mvvm:View.WidthEx="15" Margin="1" ></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Any help will be greatly appreciated.
As an aside, I have to use this exact same format (same list, etc. just different SelectedValue) in a number of forms - so if you want to suggest the best way to do this in xaml, that would be nice. In my pre-xaml days, I would just create a control, set a property or 2, and use that in all my forms. But I am supposed to use xaml, so am not sure of the best way to do this.
Thanks!
I can't believe that I searched all over the net to find an answer and, until now, could not find one. The answer is as easy as I thought it should be.
Just replace:
DisplayMemberPath="code"
with
TextSearch.TextPath="code"
and the code works just fine.
Thanks to all who helped.
I can give you an idea how to use SelectedItem
ViewModel
public class ViewModel
{
public ViewModel()
{
//Suppose your collection CitTypes is Initialized and filled with there Items
//Now you can set first Element as selected in ComboBox
SelectedItem = CitTypes.FirstOrDefault();
}
CitType selectedItem;
public CitType SelectedItem
{
get { return selectedItem; }
set { selectedItem = value; RaisePropertyChanged("SelectedItem"); }
}
}
xaml
<ComboBox Name="cboCit_type"
IsSynchronizedWithCurrentItem="True"
mvvm:View.FlowsWithPrevious="True"
ItemsSource="{Binding Path=cuCodeInfo.CitationTypes}"
Text="{Binding cit_type}"
IsEditable="true"
IsReadOnly="false"
**SelectedItem="{Binding SelectedItem}"**>

AutoCompleteBox in wpf toolkit display items in "rhino.commom.DaataContract.ClassName" form

<WPFToolkit:AutoCompleteBox
x:Name="atbParentGroup"
IsTextCompletionEnabled="True"
FilterMode="ContainsOrdinal"
ItemsSource="{Binding DataContext.Data.ParentProductGroupList}"
SelectedItem="{Binding DataContext.Data.SelectedParentProductGroup}"
ValueMemberPath="ParentGroupName"
Text="{Binding ParentGroupName}"
Width="120"
HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="0" Grid.Column="1" Margin="5,5,0,0" />
Now I got it. Ensure that the property ParentGroupName you have set as ValueMemberPath delivers a nice result - e.g. a string.
If that is not the case you can provide the full path to the property you want to display:
ValueMemberPath="ParentGroupName.FullName"
or something like that.
Another possibility would be to override the .ToString()-method of the ParentGroupName to return something readable.
Additionally make sure the Text-property does not conflict with the others, if in doubt, don't bind it at all. The SelectedItem-property will do its job.

How can I achieve a binding path which is bound by another binding?

I'm writing my own UserControl which displays data in a ListBox. I want to achieve something similar to a property like "DisplayMemberPath". In my example, it's "EditableMemberPath", which should determine which member will be displayed in the textbox. However, using a binding on a binding does not work.
<ListBox ItemsSource="{Binding Path=Items, ElementName=me}"
SelectedItem="{Binding Path=SelectedItem, ElementName=me}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Converter={Binding Path=ItemToImageConverter, ElementName=me}}" />
<TextBlock Text="{Binding Path={Binding Path=EditableMemberPath, ElementName=me}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Can you help me finding a proper solution?
Best Regards
Oliver Hanappi
You can't do that directly in XAML (at least not with the built-in classes) : a Binding is not a DependencyObject, so you can't bind its properties.
However the post mentioned by Martin Harris looks promising...

WPF Combobox DisplayMemberPath

Ok, I looked at other questions and didn't seem to get my answer so hopefully someone here can.
Very simple question why does the DisplayMemberPath property not bind to the item?
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>
The trace output shows that it is trying to bind to the class holding the IEnumerable not the actual item in the IEnumerable. I'm confused as to a simple way to fill a combobox without adding a bunch a lines in xaml.
It simply calls the ToString() for the object in itemssource. I have a work around which is this:
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" SelectedItem="{Binding Prompt}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But in my opinion it's too much for such a simple task. Can I use a relativesource binding?
DisplayMemberPath specifies the path to the display string property for each item. In your case, you'd set it to "Name", not "{Binding Name}".
You are not binding to the data in the class, you are telling it to get it's data from the class member that is named by the member "name" so, if your instance has item.Name == "steve" it is trying to get the data from item.steve.
For this to work, you should remove the binding from the MemberPath. Change it to MemberPath = "Name" this tells it to get the data from the member "Name". That way it will call item.Name, not item.steve.
You should change the MemberPath="{Binding Name}" to MemberPath="Name". Then it will work.
You could remove DisplayMemberPath and then set the path in the TextBlock.
The DisplayMemberPath is really for when you have no ItemTemplate.
Or you could remove your ItemTemplate and use DisplayMemberPath - in which case it basically creates a TextBlock for you.
Not recomended you do both.
<TextBlock text="{Binding Path=Name, Mode=OneWay}"
Alternatively you don't need to set the DisplayMemberPath. you can just include an override ToString() in your object that is in your PromptList. like this:
class Prompt {
public string Name = "";
public string Value = "";
public override string ToString() {
return Name;
}
}
The ToString() will automatically be called and display the Name parameter from your class. this works for ComboBoxes, ListBoxes, etc.
Trying this :
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" SelectedItem="{Binding Prompt}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
from what i can figure,
"DisplayMemberPath" uses reflection to get the property name in the data context class, if it cant find it nothing will be displayed.
if class
class some_class{
string xxx{ get; }
}
DisplayMemberPath=xxx, will show whatever value "xxx" is
if you want to concatenate properties from the datacontext you need to create an item template, which will show up in the header and the drop down list.
<ComboBox.ItemTemplate>
<DataTemplate DataType="employee">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding first_name}" />
<TextBlock Text="" />
<TextBlock Text="{Binding last_name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
you cannot have "DisplayMemberPath" and "ComboBox.ItemTemplate" set at the same time.

How to find a ListView in a DataTemplate?

I have this ListView in xaml
<ListView x:Name="PersonsListView" ItemsSource="{Binding}" ItemTemplate="{DynamicResource personLayout}">
<ListView.Resources>
<DataTemplate x:Key="personLayout" DataType="Person">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=FullName}"/>
<ListView x:Name="AddressesListView" ItemsSource="{Binding Path=Addresses}"/>
</StackPanel>
</DataTemplate>
</ListView.Resources>
</ListView>
How can I use AddressesListView in code behind?
For instance if I want to do AddressesListView.SelecItem.
Chk out this blog post for more scenarios like this.....
http://blogs.msdn.com/wpfsdk/archive/2007/03/16/how-do-i-programmatically-interact-with-template-generated-elements-part-i.aspx
http://blogs.msdn.com/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx
Given an item in the PersonsListView that is of Type Person, you can use the ItemContainerGenerator property on the PersonsListView, and find the container for the data item. You should then bable to use FrameworkElement.FindName(), to find that specific element.
The nested listview looks kinda weird BTW :)

Resources