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().
Related
I've got a ComboBox which looks like this:
<ComboBox x:Name="genreComboBox" ItemsSource="{Binding}" SelectionChanged="genreComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I'm binding it to a list of Genres List<Genre>, which have a Name and a genreId. Whenever the selection changes, I update the DataContext of a GridView based on that id. So basically I need to display the name, and use the id in a DataContext=someDB.getStuffById(int genreId);
I've tried messing around with getStuffById(genreComboBox.SelectedItem) and getStuffById(genreComboBox.SelectedValue), setting DisplayMemeberPath="Name" and SelectedValuePath="GenreId". Most of the time, the named get displayed.
Whatever I try to get that genreId out, I always get a NullReferenceException.
Thank you!
You may try calling getStuffById by the orignal list of genres.
getStuffById(listofGenres[genreComboBox.SelectedIndex].GenreId);
I have a combobox, which is bound to a dictionary of
<int, MyObject>
by the following code:
this.comboBox.ItemsSource= dictionary;
MyObject has some public properties, such as Name (string) and Selected (boolean).
In my Xaml I have an ItemTemplate on comboBox:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Key, Mode=OneWay}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
When I run the app, I get the combobox filled with [1, MyObject] values.
What I eventually want to do is have a checkbox and a textblock in the ItemTemplate, which will be bound to MyObject.Selected and MyObject.Name. At the moment, I can't even get the datatemplate to display the dictionaty Key value.
Any ideas why the itemtemplate is being ignored? I've tried the full temeplate (stack planel containing a combobox and textblock), that showed the same values at runtime.
cheers.
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}"
I have my data grid "dgSubsytem" column defined like below
<my:DataGridComboBoxColumn x:Name="cmbSubSysSupplier_SRV" Header="Supplier" Width="160"
ItemsSource="{Binding RelativeSource}" SelectedValueBinding="{Binding SupplierId}" />
As you see from the code i am having a combo box inside a grid .
Item source of this combo box is a datatable which is bound to it in the code behind .
Item source of the grid also another datatable bound in code behind .
code of binding item source of combobox in code behind is as follows
cmbSubSysSupplier_SRV.ItemsSource = dsComboBox.Tables[3].DefaultView;
cmbSubSysSupplier_SRV.DisplayMemberPath="FullName" ;
cmbSubSysSupplier_SRV.SelectedValuePath = "SupplierId";
Problem is combo box itself not rendering . But I can see the value of the Supplier rendered as text . What is the problem?
There are 2 parts here:
List of values to be populated in ComboBox: ItemsSource, should be bound using a StaticResource, with a List<X> fields exposed from your ViewModel.
The actual value (here X) should be bound to SelectedItemBinding using binding to data item.
No binding in code behind required.
At what point does your code-behind stuff run?
You're setting the ItemsSource in two places - in the XAML and in the Code-Behind. Whichever one runs second will overwrite the value of the first one, so only the last value set will be used.
I suspect your XAML is getting run last, and RelativeSource is probably not a property on your DataContext, so your ComboBox ends up being bound to nothing.
To fix it, simply remove your ItemsSource binding in the XAML for the DataGridComboBoxColumn
<my:DataGridComboBoxColumn x:Name="cmbSubSysSupplier_SRV"
Header="Supplier" Width="160"
SelectedValueBinding="{Binding SupplierId}" />
In addition, the DefaultView of a DataTable will return an object of type DataView, and DataView does not have properties called FullName or SupplierId, so your SelectedValuePath and DisplayMemberPath properties won't work.
I'd recommend building a list of KeyValuePair<int,string> out of your data items, and bind your ComboBoxColumn.ItemsSource to that list, then switch the SelectedValuePath to "Key" and the DisplayMemberPath to "Value"
I personally fought with DataGridComboBoxColumn for long time and i think the way is to use DataGridTemplateColumn. Here is an exemple :
Looks a lot of code but evective.
Put the collection as resource :
<Grid.Resources>
<CollectionViewSource x:Key="StructuresCollection" Source="{Binding StructuresList, Mode=OneTime}"/>
</Grid.Resources>
<DataGridTemplateColumn Header="Structure" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Structures.Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox x:Name="CStructures" SelectedItem="{Binding Structures}" DisplayMemberPath="Name" SelectedValue="{Binding IDStructure, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="{Binding IDStructure}" ItemsSource="{Binding Source={StaticResource StructuresCollection}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
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.