I've spent hours trying to figure out what is happening to my selecteditem property of my ComboBoxes and Im pretty sure I know what is causing the problem now, but I just dont understand why this is happening and if there is something I can do about it.
Basically I have a DataGrid with comboboxes in the headers that I use for filtering the DataGrid items. The DataGrid sits in a Tabcontrol and if I select items in the comboxes and switch tab and back the selected items are removed.
This happens only when the ItemsSource of the Comboboxes have the IsAsync property set to True. Otherwise everything works just like I want it to.
To give some more info about the logic:
The comboboxes get their values from a IValueConverter its the same converter for all comboboxes and I pass a ConverterParameter to tell it what to return. The collection sent to the converter is the items shown in the datagrid.
These values are refreshed (each item has selected parameters updated, the collection is not cleared and recreated) when the tab is activated.
This my xaml for one of the comboboxes:
<ComboBox SelectedItem="{Binding MadeBy}" ItemsSource="{Binding IssuesView,Converter={StaticResource DataGridFilterableValueConverter}",ConverterParameter=Madeby, IsAsync=true}" SelectionChanged="FilterComboBox_SelectionChanged"/>
Now, like i described previously, if I set the IsAsync to false the selecteditems stay.
Do I just have to live with this? We work alot with sql-databases and IsAsync really makes the interface alot more snappy. I can live without it in the comboboxes, but I tend to use it as much as I can.
I've seen that ppl have been having problems with the selecteditem and the general solution seems to be placing the SelectedItem before the ItemsSource, but this does not help.
Thanks
Erik
Edit 1:
Here is the xaml of the tabcontrol and tabitem.
<TabControl Grid.Row="1" x:Name="TabControl" SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Granskningssynpunkter" x:Name="IssueTab">
..... Lots of code here but nothing that concerns TabControl or TabItem
Related
After hours and hours of googling, I still can't find a simple solution for binding an ObservableCollection to the selected items of a ListBox in a TwoWay Mode...
What I have is really simple : a ListBox with SelectionMode="Multiple", and an ObservableCollection<Contact> named SelectedContacts. I want this two to be bound. Of course my ListBox has ItemsSource="{Binding Contacts}" which is another ObservableCollection of Contact.
Now I really can't use a IsSelected bool on my Contact items, I just can't.
Thank you !
There is no simple solution. You can't bind SelectedItems.
The best solution is to select your Contact items into a view model object with an IsSelected property, bind to that, and then run a query against the primary OC when you need to get the selected items collection.
Since you said you can't/won't do that, the next best solution would likely be to handle SelectionChanged in your code-behind and manually update the VM collection from there.
Since SelectedItems is not a DependencyProperty you are not allowed to use Bindings
But there is a solution, take a look at this post:
http://blogs.microsoft.co.il/miziel/2014/05/02/wpf-binding-listbox-selecteditems-attached-property-vs-style/
Another option is to not keep track of the selected items in your ViewModel. Instead, pass them as a CommandParameter from the UI when you are trying to do some action.
Example:
<ListBox x:Name="MyListBox"
ItemsSource="{Binding SomeCollection}" />
<Button Command="{Binding SomeCommand}"
CommandParameters="{Binding SelectedItems, ElementName=MyListBox}" />
I'm very new to WPF. I'm trying to bind to a property a row in a DataGrid so that when the row's clicked the property is set. The ItemsSource that's bound to the DataGrid is an ObservableCollection of objects of type Field.
I've tried to bind to the SelectedItem attribute on the DataGrid, but the property is not being called. I'm using almost identical code to bind to the SelectedItem of a ComboBox and this is working fine. Is there a difference that I don't know about?
<ComboBox ItemsSource="{Binding RecordTypes}" SelectedItem="{Binding SelectedRecordType}" ...
<DataGrid ItemsSource="{Binding Fields}" SelectedItem="{Binding SelectedField}" ...
In my ViewModel:
private Field SelectedField
{
get
{
return _selectedField;
}
set
{
_selectedField = value;
}
}
(I will use auto properties later, it's just currently set up like this so that I could break when the property was set).
I'm not sure if it makes a difference, but the DataGrid is composed of 2 DataGridTextColumns and a DataGridTemplateColumn, which contains a checkbox.
Does anyone have any ideas? I'd really appreciate any suggestions.
To confirm, the reason that I want to listen to the click of a row is so that I can have the checkbox be checked whenever a row is clicked. If there's a better solution for this then please let me know.
You need to make it a two-way binding:
SelectedItem="{Binding SelectedField,Mode=TwoWay}"
That propagates changes in the view (user selects an item, SelectedItem changes) back to the viewmodel ("SelectedField" property).
Also, as #KevinDiTraglia pointed out, you need to make sure that the viewmodel property SelectedField is public, not private, otherwise the binding will not be able to access the getter/setter.
I have a collection of my custom entity that is bound to the listpicker using the ItemsSource property. I also have selection mode set to Multiple so I have a checkbox with each item in the FullMode picker. This picking works, fine, and I can easily access all objects that were picked thru code. What I'm having troubles with is the DisplayMemberPath. I want to display something more friendly than the namespace of the object that is selected. Perhaps a count of selected items, or a comma separated list of the values selected.
Unfortunately, 'AccountId' doesn't work when I set the SelectionMode="Multiple". Single mode is fine. Any ideas?
<toolkit:ListPicker
x:Name="accountlistpicker"
Grid.Row="0" Header="accounts"
SelectionMode="Multiple"
DisplayMemberPath="AccountId"
ItemsSource="{Binding AllAccounts}"
FullModeItemTemplate="{StaticResource AccountsListPickerFullItemTemplate}" />
You need to assign function to SummaryForSelectedItemsDelegate that will process how summary string will looks like.
Check this for learn more
Im cleaning up this question and going to put in an example that makes sense to me..hopefully for you too...
so lets say i have an Items control. This control is bound to an observable collection of NBA Basketball teams (Lakers, Heat, Knicks...ect). In this observable collection of NBATeams I have a property of FavoritePlayer. I have a datatemplate that lays out how this is going to look inside the Items Control. I have a textbox which displays the team name. (this could be/should be read only..its not now) but it displays the team name.. LA Lakers. the second item in my items control is a combobox. this combo box will ultimately display my favorite player on each team.
The itemssource on the combobox is from a lookup value i have. It displays all the people on the team and the displaymemberpath on this combobox is "DisplayText"...I need my selected item on this combobox to be shown. so if my favorite player is Kobe Bryant the combo box should display this.
<telerik:RadComboBox Grid.Row="0" Grid.Column="9" Width="150" EmptyText="--Select Player--"
ItemsSource="{Binding PlayerList}"
SelectedItem="{Binding FavoritePlayer, Mode=TwoWay}"
DisplayMemberPath="DisplayText" HorizontalAlignment="Left"></telerik:RadComboBox>
I am used to just having textblocks in a listbox and then when an item is selected from within that listbox i use that selected item to bind my comboboxes that live outside of my listbox. Now i actually have the combobox in the listbox(ItemsControl now). So Im wondering the best way to bind the combobox from within an ItemsControl
I initially was just having issues getting the players to show up in the combobox..but I solved that by doing the code below.
I found my first issue of just binding the items source to the combo box from within an items control.
I had to set the bindings to look like this.
ItemsSource="{Binding DataContext.PlayerList, ElementName=ItemsControlNBATeams}"
so i had to set the name of the items control and use that in the element name and set the path = to DataContext.PlayerList. this now brings up the items in my combo box..what it doesnt do is set the selecteditem of my combo box.
Not very clear about your you are asking but does this answer your question
<ListBox ItemsSource="{Binding}" x:Name="ListBox1"></ListBox>
<TextBox Text="{Binding SelectedItem, ElementName=ListBox1}"> </TextBox>
I have a DataGrid with CurrentItem bound on a property. DataGrid has implemented Paging.
If I select items in the first page, CurrentItem changes and everything is fine. However, when I set another page... I change Observable collection to display another 30 rows, the CurrentItem binding does not sork.
I have:
<DataGrid CurrentItem="{Binding CurrentItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Orders}">
I'm not sure, I'm following your question - but, I'll take a wild guess :-). I'm guessing your problem is that the CurrentItem is no longer set (and no row is selected in the DataGrid) unless you manually select a new item in the DataGrid? If that is the case, you simply need to add a property in your DataGrid:
<DataGrid IsSynchronizedWithCurrentItem="True"/>
This will force the DataGrid to select the first item when data is reloaded.
Another wild guess:
When you set the new source - how do you do that? if you update your property with a new ObservableCollection without calling a PropertyChanged event your binding will no longer work (but then, your DataGrid shouldn't update either).
Hope, either of those help - otherwise, I need more info to help you :-).