I have a table in my database that represents a list of areas. I would like to get all of these items in this list and bind them to a combobox dropdown in my datagrid. This currently works, and my grid column displays this dropdown. However, when I select an item in the cell and move to the next row, the cell becomes blank. Also, if the entry in the database has a saved value of "area1", and my dropdown list has "area1,area2,area3,etc", when the grid loads, it doesn't autoselect area1, the cell is simply blank.
In my model:
comboboxColumn1.ItemsSource = ctx.AREAS;//db context loading all areas into combobox
In my xaml:
<DataGrid.Columns>
<DataGridComboBoxColumn DisplayMemberPath="Name" Header="some header" x:Name="comboboxColumn1" SelectedValueBinding="{Binding Name}" />
....
How would I go about setting this selected value so that it is equal to whatever entry is in the db?
You need to set the SelectedValuePath value to your variable property. The DisplayMemberPath is for what is shown, and SelectedValuePath is what is selected. SelectedItem is the actual item that is selected.
Related
I have a ComboBox which takes it's values from ObservableCollection which gets it's data from a XML file.
I am able to display the data from XML to the ComboBox whose code is :
<ComboBox x:Name="ComboBox1" ItemSource="{Binding Path = <Property Name>}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}">
Now I want to make modifications such that only some Items in the ComboBox have a checkbox which will be based on the text of the ComboBox Item.
If I use Data Template in ComboBox.ItemTemplate, then all the items of combobox will have the checkbox which I don't want.
Is there any way I can achieve this?
In the ItemTemplate, use the DataTemplate in which there will be triggers that change the Visibility of the flag according to the conditions you need.
I have the editable ComboBox that using binding to dictionary:
<ComboBox IsEditable="True" ItemsSource="{Binding MyDict}" DisplayMemberPath="Value" SelectedValuePath="Value" SelectedValue="{Binding MyProp}" />
Sometimes I need set in code to property MyProp some different value, that is not presented in the dictionary. In such situation ComboBox is not dispalay the value.
What should I do to ComboBox be able to display arbitrary value in this case?
A ComboBox cannot select arbitrary values. It can only select a value or item that is present in its Items or ItemsSource collection.
So you must add an entry to MyDict before you can select it. You may set the Text property of the ComboBox to a random string, but this won't select any item and set the MyProp property.
When my app starts, there is a ListView populated with data. How do I set it so that the first row is not selected automatically? Ie I don't want any items to be selected until the user clicks one
I have tried:
setting the property bound to the ListView's SelectedItem to null (the property is bound two-way),
setting the FallBackValue of the SelectedItem to null
but neither of these worked. Is there a way to do this, or am i stuck with the first item being selected when the app starts?
If you dont put SelectedItem or SelectedIndex it is not selecting anithing, but you want to set than try this code:
<ListView IsSynchronizedWithCurrentItem="True">
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
<ListViewItem>Some text1</ListViewItem>
</ListView>
How can I maintain selection on a combobox if the currently selected item is replaced in the ItemsSource collection? In this case the collection is an ObservableCollection and of course if the currently selected item is replaced the combobox loses its selection - nothing is selected.
The ComboBox looks like:
<ComboBox
Name="combobox"
SelectedValuePath="Id"
DisplayMemberPath="Description"
SelectedValue="{Binding Source={StaticResource cvs}, Path=Id, Mode=TwoWay}"/>
I cannot simply set the selected item on the combobox manually each time as the collection is manipulated in another generic class I cannot touch!
Thanks!
you bind the selectedVaule to your Id Property. so if you want the new added Item to be the selected one, just set your Id property to the new Item and call OnPropertyChanged("Id")
myCollection.Remove(oldItem);
myCollection.Add(newItem);
Id = newItem;
OnPropertyChanged("Id")
Use this code to select the new item:
combobox.SelectedItem = newItem;
UPDATE:
If the combobox is unknown to the part of the code that replaces the item, you need to do something like the following:
Subscribe to the CollectionChanged event of your collection.
When the event is fired and it says that a new item was added, execute the code as shown above.
I have a combobox in a gridview in my silverlight application.
<Controls1:GridViewComboBoxColumn Header="Accomplishment Category"
ItemsSource="{Binding AccomplishmentCategoryList}"
DataMemberBinding="{Binding AccomplishmentCategoryValue}"
SelectedValueMemberPath="{Binding AccomplishmentCategoryValue}">
</Controls1:GridViewComboBoxColumn>
I am able to get my grid to display the results for its own itemSource.
<Controls1:RadGridView x:Name="Accomplishments" Grid.Row="1" CanUserInsertRows="True"
ShowInsertRow="True" CanUserDeleteRows="True" RowIndicatorVisibility="Visible"
IsReadOnly="False" ItemsSource="{Binding AccomplishmentResults, Mode=TwoWay}">
The AccomplishmentResults collection binds perfectly. my issue is the combobox does not display anything until you actually click on the column that contains the combobox control. so that column looks empty, when you click on the column the results are displayed, when you click again you get the combobox that will show the items in that collection..if you select anything other than that column those values disappear and are empty again. if you select the row they remain empty..it is only when the column is selected that the values appear.
By default ListView and DataGrid only displays text till the item is clicked and item becomes editable.
You will have to create Templated Column, that will always display combobox as declared in template.