WPF Combobox how to display value in LIST Collection - c#

I have a List collection that when it is assigned has a collection of type short numbers. However how do I display the numbers on a WPF ComboBox?
I usually use the DisplayMemberPath property in XAML, but I cannot state a property because it isn't my own collection. (Such as List(T)....)
The XAML
<ComboBox HorizontalAlignment="Left"
ItemsSource="{Binding TheList}"
// This is wrong of course
DisplayMemberPath="."
C#
public List<short> TheList
{
get
{
return m_ListSID;
}
set
{
m_ListSID = value;
}
}
Any help would be grateful.
EDIT
Utter silliness...All answers are of course true and makes sense, it was just due that I forgot to trigger the update source for the Collection
ItemsSource="{Binding TheList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Apologises
Thanks

DisplayMemberPath is not needed here
<ComboBox HorizontalAlignment="Left" ItemsSource="{Binding TheList}" DisplayMemberPath="" />

You don't need to have any DisplayMemberPath
This is sufficient:
<ComboBox HorizontalAlignment="Left" ItemsSource="{Binding TheList}"/>

Don't set DisplayMemberPath at all and short should be converted to string
<ComboBox HorizontalAlignment="Left" ItemsSource="{Binding TheList}"/>
if no template is provided by default WPF will call ToString() on an item.
EDIT
You can find more details on how display content is created on the ContentPresenter MSDN page

Related

How to load ItemsSource before setting SelectedItem in ListView?

I have a MVVM page that contains a ListView. I bind ItemSource and SelectedValue, but first time it calls converter for SelectedValue then loads ItemSource.
<ListView x:Name="ListViewSurahs"
ItemsSource="{Binding MyItems}"
FlowDirection="LeftToRight"
Grid.Column="2"
Grid.Row="4"
VerticalAlignment="Top"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
SelectionMode="Single"
ScrollViewer.VerticalScrollBarVisibility="Auto"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{Binding Source={StaticResource CurrentInfo},
Path=Instance.ID,Mode=OneWay}"
ShowsScrollingPlaceholders="False" />
because of that I lose SelectedItem and no Items get selected. what should I do to load ItemsSource first?
Firstly you can try to type your ItemsSource in codebehind. You have to add an UserLoaded property in your xaml file to do this. But maybe we need to see your codebehind and viewmodel. Anyway you should try to change your SelectedValue binding mode OneWay to the TwoWay.
After that you should watch your binding style. You have to complete most of your development progress in your viewmodel, and after that you could just call your viewmodel from your xaml codebehind (.cs) with get-set. So, you will have a very clean binding structure.
In this way you can type as follows instead of yours,
SelectedValue="{Binding Model.BlaBla, Mode=TwoWay}"
in here Model is defined and called in your codebehind of xaml file (.cs). For example in top of your public sealed partial class
public YourViewModelName Model { get; set; }
and in the same file public YourXamlName()
Model = new YourViewModelName();
It is a quickly answer and I am not sure. But you should give a shot.
Good luck.

WPF Combobox binding with List<string>

I have two properties, one which is a list of string and the other just a string.
private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}
public String SimpleStringProperty{get;set;}
I also have a Combobox defined in XAML as such
<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />
Now the combobox correctly displays two options :"string1" and "string2"
When the user selects one or the other, I want to set SimpleStringProperty with that value. However, the 'value' im getting back from the combobox through the two way binding is not the selectedItem, but the List<String>. How can I do this right? I'm fairly new to wpf, so please excuse the amateurism.
<Combobox ItemsSource="{Binding Property}" SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" Text="Select Option" />
That's untested, but it should at least be pretty close to what you need.
You need to bind to the String property using the SelectedItem property of the combobox.
<Combobox ItemsSource="{Binding Property}"
SelectedItem="{Binding SimpleStringProperty}"
IsSynchronizedWithCurrentItem="True"
Text="Select Option" />
What helped me:
Using SelectedItem
Adding UpdateSourceTrigger=PropertyChanged
IsSynchronizedWithCurrentItem="True" to be sure Selected item always synchronized with actual value
Mode=TwoWay if you need to update as from source as from GUI
So at the end best way, if source is
List<string>
Example:
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding SomeBindingPropertyList}"
SelectedItem="{Binding SomeBindingPropertySelectedCurrently,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Additional Info
Difference between SelectedValue and SelectedItem:
https://stackoverflow.com/a/4902454/2758833
https://stackoverflow.com/a/2883923/2758833
SelectedValuePath Documentation:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.selector.selectedvaluepath
SelectedValue updates possible bugs for .NET 4 and .NET 4.5:
https://stackoverflow.com/a/247482/2758833

selectedItem is not working in xaml

i am trying to set selected item through following code but its not working:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Sort by" Margin="10" VerticalAlignment="Center"/>
<ComboBox Width="{StaticResource ComboWidth}" x:Name="sortcombo" ItemsSource="{Binding Path=SortOrder}" SelectionChanged="SearchCombo_SelectionChanged" SelectedItem="{Binding Path=DefaultSortIndex}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Sort}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
however it works fine if i use selectedIndex instead with binding to 0th index. Any thing wrong with declaration?
By the name of your property DefaultSortIndex maybe you are trying to bind an int for SelectedItem.
SeletedItem refers to an element of your collection binded to ItemsSource, so the property binded to SelectedItem must be of type of your collection elements.
If you bind int value to selected item then it will not work, you should bind element for that. For int value you can set it as mentioned in following post :
Set Selected Item of WPF Combobox to User Setting
found out the issue, actually the data source was creating new list everytime I call getData().

How to Bind with Combobox in silverlight

I want to bind my collection of countries to a Combobox.
My XAML page looks as follows:
<pmControls:pmComboBox
Margin="3" Grid.Row="5" Grid.Column="1"
ItemsSource="{Binding Path=Countries}"
SelectedValue="{Binding Title,Mode=TwoWay}" >
</pmControls:pmComboBox>
I want to display Title as DataTextField.
Currently it shows namespace of country class in my combobox list .
I have also tried to add DisplayMemberPath but it also not works.
How can I setup the display field of Combobox to use a Binding?
I think you need to do
DisplayMemberPath="Title"
SelectedValuePath="Title"
SelectedValue="{Binding Path=Country,Mode=TwoWay}"

Bind ObservableCollection<XmlNode> to combobox wpf

I'm trying to bind a combobox to a ObservableCollection.When the form is displayed the combobox is empty.The same code with ObservableCollection of type string works perfectly. I've got a feeling that my XPath is wrong. Any suggestions are welcome:
XAML:
<ComboBox ItemsSource="{Binding ItemParameters, XPath=InnerXml/name,Mode=TwoWay}" SelectedIndex="0" Margin="2" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="1" Height="24" />
ObservableCollection XmlNode :
public ObservableCollection<XmlNode> _itemParameters = new ObservableCollection<XmlNode>();
public ObservableCollection<XmlNode> ItemParameters
{
get { return _itemParameters; }
set { _itemParameters = value; }
}
The combobox should display the name attribute of each XmlNode in the collection:
Update:
I've tried using DisplayMemberPath in two different ways, but the combobox still contains no data:
DisplayMemberPath="{Binding XPath=name}" ItemsSource="{Binding ItemParameters}"
DisplayMemberPath="{Binding XPath=InnerXml/name}" ItemsSource="{Binding ItemParameters}"
Solution:
This did the trick, hope it helps someone else as well:
<ComboBox DisplayMemberPath="#name" ItemsSource="{Binding ItemParameters}"
First of all you are setting Path and XPath at the same time, which are conficting properties, secondly you bind the ItemsSource, which has nothing to do with what you want to show inside the item. Either use DisplayMemberPath or an ItemTemplate for that, the ItemsSource should just be bound to ItemParameters.

Categories