WPF ComboBox not updating source - wpf

I'm trying to get a WPF combobox working (inside the WPFToolkit Datagrid), and I'm having trouble getting all the pieces aligned correctly. I'm using Linq to Entities, and I'm setting the overall datacontext to the results of a Linq query:
private void LoadDonationGrid()
{
donationGrid.ItemsSource = from donations in entities.Donation
.Include("Family")
.Include("PledgeYear")
.Include("DonationPurpose")
from donationPurposes in entities.DonationPurpose
select new { donations, donationPurposes };
}
I also have a page property in my code-behind which provides the ItemsSource for the combobox:
private IEnumerable donationPurposeList;
public IEnumerable DonationPurposeList
{
get
{
if (donationPurposeList == null)
{
donationPurposeList = from dp in entities.DonationPurpose
select dp;
}
return donationPurposeList.ToList();
}
}
The XAML for the combobox looks like this:
<tk:DataGridTemplateColumn Header="Purpose">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=donations.DonationPurpose.Description, Mode=TwoWay}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="cboDonationPurpose"
SelectedValue="{Binding Path=donations.DonationPurposeID, Mode=TwoWay}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page},Mode=FindAncestor},Path=DonationPurposeList}"
DisplayMemberPath="Description"
SelectedValuePath="DonationPurposeID"
/>
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
</tk:DataGridTemplateColumn>
And everything seems to work correctly, i.e., the appropriate values are displayed in the ComboBox, right up to the point where focus leaves the ComboBox. At that point, the displayed value returns to the original value, not to the newly selected value. I've tried using SelectedItem instead of SelectedValue, but that ends up with the selected value not showing in the ComboBox. I'm kinda mystified: it seems like this bit should be working.
Edited 3/2/09: I'm still puzzling over this. I should note that in my datagrid, any simple data columns (e.g., "DataGridTextColumn") update the underlying data source just as you'd expect. But any update to any of my templated columns ("DataGridTemplateColumn") or ComboBox columns ("DataGridComboBoxColumn") don't work: the underlying data source never gets updated. Surely other folks have tried to use the WPF.Toolkit datagrid and gotten this scenario to work -- but I've looked at all the sample code out there, and I'm doing basically what it says to do (within the constraints of my solution), so I'm scratching my head why this isn't working.
Any thoughts?

I had a similar problem (on which I spent days of frustration), and I found that changing the UpdateSourceTrigger on the SelectedValue binding to PropertyChanged fixed it. I don't know why, but for me, the datasource wasn't being updated until I made this change.
<ComboBox
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UpdateTypesManager:MainWindow}}, Path=CardinalityTypes}"
DisplayMemberPath="CardinalityType"
SelectedValue="{Binding CardinalityTypeId, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id" />

I was able to get this working. But I set things up a wee bit differently.
I created a ViewModel to act as a contract with the View.
I bound to the ComboBox.SelectedItem Property instead of ComboBox.SelectedValue Property
Since I didn't know what your data source was I made up my own to simulate the basic problem: having a comboBox bind correctly within a WPF DataGrid.
Here is the composition of my View Model:
public class RootViewModel
{
public List<State> USStates { get; set; }
public List<Customer> Customers { get; set; }
public ViewModel()
{
Customers = new List<Customer>();
Customers.Add(new Customer() { FirstName = "John", LastName = "Smith", State = new State() { ShortName = "IL" } });
Customers.Add(new Customer() { FirstName = "John", LastName = "Doe", State = new State() { ShortName = "OH" } });
Customers.Add(new Customer() { FirstName = "Sally", LastName = "Smith", State = new State() { ShortName = "IN" } });
USStates = new List<State>();
USStates.Add(new State() { ShortName = "OH" });
USStates.Add(new State() { ShortName = "IL" });
USStates.Add(new State() { ShortName = "IN" });
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public State State { get; set; }
}
public class State
{
public string ShortName { get; set; }
public State()
{
ShortName = string.Empty;
}
public override bool Equals(object obj)
{
if (obj is State)
{
State otherState = obj as State;
return ShortName.Equals(otherState.ShortName);
}
else
{
return false;
}
}
}
Before we begin, I set the DataContext of my Window to be an instance of my properly constructed RootViewModel.
<tk:DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False">
<tk:DataGrid.Columns>
<tk:DataGridTemplateColumn Header="State">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=State.ShortName, Mode=TwoWay}" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
<tk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
x:Name="cboDonationPurpose"
SelectedItem="{Binding Path=State, Mode=TwoWay}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}, Path=DataContext.USStates}"
DisplayMemberPath="ShortName"
SelectedValuePath="ShortName" />
</DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>
</tk:DataGridTemplateColumn>
</tk:DataGrid.Columns>
</tk:DataGrid>
In order for the SelectedItem to bind properly I need to ensure that I have overriden the Equals method on my entity since under the hood, WPF is using this method to determine who is the SelectedItem or not. I think this was fundamentally your problem from the beginning which caused you to try to bind to SelectedValue instead of SelectedItem.

You need to add
IsSynchronizedWithCurrentItem = "True"
in your Xaml.
It's as simple as that...

Related

ComboBox SelectedItem initial value not from ItemsSource

I cannot understand the behavior of the combobox.
I have an edit view to edit existing Order data. Here is my VM of some part of Order data:
public class OrderDataViewModel : ViewModelBase, IOrderDataViewModel
{
private readonly ICustomersListProviderService _customersListProviderService;
private readonly Order _order;
public OrderDataViewModel(Order order, ICustomersListProviderService customersListProviderService)
{
Assign.IfNotNull(ref _order, order);
Assign.IfNotNull(ref _customersListProviderService, customersListProviderService);
}
public DateTime CreationDate
{
get { return _order.CreationDate ?? (_order.CreationDate = DateTime.Now).Value; }
set
{
if (_order.CreationDate == value) return;
_order.CreationDate = value;
OnPropertyChanged();
}
}
public Customer Customer
{
get { return _order.Customer; }
set
{
if (_order.Customer == value) return;
_order.Customer = value;
OnPropertyChanged();
}
}
private IList<Customer> _customersList;
public IList<Customer> CustomersList
{
get { return _customersList ?? (_customersList = _customersListProviderService.GetAllCustomers().ToList()); }
}
}
And XAML binding:
<ComboBox Grid.Row="2" Grid.Column="1"
SelectedItem="{Binding OrderDataViewModel.Customer}"
DisplayMemberPath="Name"
ItemsSource="{Binding OrderDataViewModel.CustomersList}"
/>
Description. Order comes from the database by the Repository, _customersListProviderService gets all customers from database also. I know that maybe it could be done better, but it's not the point of the question.
And... the issue is. After loading a window, my combobox has items collection filled (dropdown list is not empty) but the value is not set (its blank). Checking SelectedItem by code-behind results with null. I read a lot and found out, that you cannot set SelectedItem of combobox to the item that is not in ItemsSource.
Ok, my workaround was to change the Customer getter to:
public Customer Customer
{
get
{ return CustomersList.Single(c => c.Id == _order.Customer.Id); }
set
{
if (_order.Customer == value) return;
_order.Customer = value;
OnPropertyChanged();
}
}
now it works, but it does not look good to me.
Is there any better solution?
you can override Equals() and GetHashCode() in your entities and return Id.Equals() and Id.GetHashCode() respectively
I just had a similar issue within an UWP app. I was binding to a string array.
<ComboBox SelectedItem="{Binding Carrier, Mode=TwoWay}" ItemsSource="{Binding Carriers}" />
The problem solved when I changed ItemsSource to be before SelectedItem:
<ComboBox ItemsSource="{Binding Carriers}" SelectedItem="{Binding Carrier, Mode=TwoWay}" />
Just in case someone has a similar issue.
Perhaps adding SelectedValuePath="Name" to your xaml like below will help
<ComboBox Grid.Row="2" Grid.Column="1"
SelectedItem="{Binding OrderDataViewModel.Customer}"
DisplayMemberPath="Name"
ItemsSource="{Binding OrderDataViewModel.CustomersList}"
SelectedValuePath="Name"
/>
Create a ViewModel with both sets of data you need i.e set to fill ComboBox and the Record. I will use Customer and year for convenience.
class CustomerDetailsViewModel
{
public CustomertModel CutomerModel { get; set; }
public YearListModel YearList { get; set; }
public CustomerDetailsViewModel(CustomerModel _CustomerModel)
{
CustomerModel = _CustomerModel;
YearList = new YearListModel();
}
}
So I fill the combobox with a list of years and I have my selected Customer record.
<ComboBox x:Name="cbCustomerDetailsYear" Margin="128,503,0,0"
DataContext="{Binding DataContext,
ElementName=CustomerDetailsPage}"
ItemsSource="{Binding YearList.Years, Mode=OneWay}"
DisplayMemberPath="Description"
SelectedValue="{Binding CustomerModel.YearID}"
SelectedValuePath="id" />
The viewmodel is assigned to the Page Datacontext and I bind this to the Combobox.
The combobox is populated with Itemsource and Displaymember with the Year model list from my Viewmodel. The description is just a string saying 1999, 2000 or whatever
The SelectedValue is bound to the Year foreign key in the Customer record also in the ViewModel. The SelectedValuePath is the magic ingredient that binds the 2 together. So the id represents the id of the year but will be bound to the customer YearID field and set by the Combobox id value.
Hope this is clear ?!?!?!
I hope this would work:
Check if Customer, is already binded to another Control. If so, remove all other Bindings which involve Customer.
I had the same problem where the IsChecked property of a CheckBox was also Binded to the viewModel but using a converter to check if it's null or not. I changed its binding to a boolean and the problem was no more.

DatagridComboBoxColumn.ItemsSource does not reflect changes from source other than the source bound for datagrid

ComboBox items do not reflect changes made from its source
Here is what I am trying to accomplish:
I have a WPF datagrid that binding to a database table, inside the datagrid there is a combobox(group ID) column bind to one of the columns from the database table; the combobox items are from another table(a list of group ID). The problem now is when the groupd ID list is changed from other table, the combo box items does not take effect.
Can anyone help? Have been stuct for a long time.
Here is XAML code:
<DataGridTemplateColumn Header="Group ID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding GroupID, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ComboBoxTeamGrpID" SelectedItem="{Binding GroupID, Mode=TwoWay}" ItemsSource="{StaticResource ResourceKey=GroupIDList}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Here is the code for GroupIDList:
public class GroupIDList : List<string>
{
public GroupIDList()
{
try
{
string tmp = ConfigurationManager.AppSettings["DataSvcAddress"];
Uri svcUri = new Uri(tmp);
JP790DBEntities context = new JP790DBEntities(svcUri);
var deviceQry = from o in context.Devices
where o.GroupID == true
select o;
DataServiceCollection<Device> cList = new DataServiceCollection<Device>(deviceQry);
for (int i = 0; i < cList.Count; i++)
{
this.Add(cList[i].ExtensionID.Trim());
}
this.Add("None");
//this.Add("1002");
//this.Add("1111");
//this.Add("2233");
//this.Add("5544");
}
catch (Exception ex)
{
string str = ex.Message;
}
}
}
Here is another problem related, can anyone help? thank you.
It is either because your GroupIdList is a List and not an ObservableCollection, or because you're binding to a StaticResource, which WPF assumes is unchanged so is only loaded once.
Change your List<string> to an ObservableCollection<string> which will automatically notify the UI when it's collection gets changed, and if that still doesn't work than change your ItemsSource from a StaticResource to a RelativeSource binding, such as
ItemsSource="{Binding
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
Path=DataContext.GroupIdList}"
Edit
Your parent ViewModel which has your DataGrid's ItemsSource collection should look something like below. Simply add another public property for GroupIdList and have it return your list. Then use the above RelativeSource binding to access it, assuming your DataGrid's ItemsSource is bound in the form of <DataGrid ItemsSource="{Binding MyDataGridItemsSource}" ... />
public class MyViewModel
{
private ObservableCollection<MyDataObject> _myDataGridItemsSource;
public ObservableCollection<MyDataObject> MyDataGridItemsSource
{
get { return _myDataGridItemsSource; }
set
{
if (value != _myDataGridItemsSource)
{
_myObjects = value;
ReportPropertyChanged("MyDataGridItemsSource");
}
}
}
private ObservableCollection<string> _groupIdList = new GroupIdList();
public ObservableCollection<string> GroupIdList
{
get { return _groupIdList; }
}
}
WPF will not poll everytime and check if your list changed. In Order to do this, as Rachel pointed at you should do something like :
public class GroupIDList : ObseravableCollection<string>
EDIT :
Here is my suggestion :
I actually wouldn't do it the way you did. What I do is I create a View Model for the whole grid, that looks like :
public class MyGridViewModel : DependencyObject
Which I would use as data context for my grid:
DataContext = new MyGridViewModel ();
Now the implementation of MyGridViewModel will contain a list of ViewModel that represent my GridRows, which is an ObservableCollection
public ObservableCollection<RowGridViewModel> RowItemCollection { get; private set; }
I will this in my dataGrid as such :
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding RowItemCollection}" SelectionMode="Extended" SelectionUnit="Cell">
<DataGrid.Columns>
and All you need to do, is to fill in you RowItemColleciton with the correct data, and then bind you Columns to the correct Property in RowGridViewModel...in your case it would look like (but you have to initialize the GroupIDList :
public class RowGridViewModel: DependencyObject
{
public List<String> GroudIDList { get; set;
}
}
Let me if that help

WPF MVVM Combox not retaining old value on Validation fail

i have a combox control which is bound to a property using MVVM. There is validation done in the set method on value change.. The problem is the value getting changed to new value even if the validation fails and not retaining the old value..
Below is the XAML:
<ComboBox Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding Path=Applications, Mode=OneTime}" SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.Application, Mode=TwoWay}" Margin="3"></ComboBox>
Below is the View Model Code:
private string[] types = new string[] { "A", "B" };
private string application;
public ObservableCollection<string> Applications { get; private set; }
public Const() {
this.Applications = new ObservableCollection<string>(this.types.ToList());
}
public string Application {
get {
this.application = this.applicationSpecificRequirements.ContainsKey(Resources.ApplicationKey) ? this.applicationSpecificRequirements[Resources.ApplicationKey] : this.Applications[0];
return this.application;
}
set {
if (this.exchangeViewModel.CheckIfApplicationNameExistsOrIsEmptyAndAssign(this.InstanceName, value)) {
System.Windows.Application.Current.Dispatcher.BeginInvoke(
new Action(() => {
this.applicationSpecificRequirements[Resources.ApplicationKey] = this.application;
((IHaveOnPropertyChangedMethod) this).OnPropertyChanged("Application");
}), DispatcherPriority.ContextIdle, null);
return;
}
this.applicationSpecificRequirements[Resources.ApplicationKey] = value;
}
}
looks like you're missing OnPropertyChanged(...) at last line in property setter.

Siliverlight databound combobox doesn't display initialized value

I am databinding a view to a viewmodel and am having trouble initializing a combobox to a default value. A simplification of the class I'm using in the binding is
public class LanguageDetails
{
public string Code { get; set; }
public string Name { get; set; }
public string EnglishName { get; set; }
public string DisplayName
{
get
{
if (this.Name == this.EnglishName)
{
return this.Name;
}
return String.Format("{0} ({1})", this.Name, this.EnglishName);
}
}
}
The combobox is declared in the view's XAML as
<ComboBox x:Name="LanguageSelector" Grid.Row="0" Grid.Column="1"
SelectedItem="{Binding SelectedLanguage,Mode=TwoWay}"
ItemsSource="{Binding AvailableLanguages}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and the viewmodel contains this code
private List<LanguageDetails> _availableLanguages;
private LanguageDetails _selectedLanguage;
public LoginViewModel()
{
_availableLanguages = LanguageManager.GetLanguageDetailsForSet(BaseApp.AppLanguageSetID);
_selectedLanguage = _availableLanguages.SingleOrDefault(l => l.Code == "en");
}
public LanguageDetails SelectedLanguage
{
get { return _selectedLanguage; }
set
{
_selectedLanguage = value;
OnPropertyChanged("SelectedLanguage");
}
}
public List<LanguageDetails> AvailableLanguages
{
get { return _availableLanguages; }
set
{
_availableLanguages = value;
OnPropertyChanged("AvailableLanguages");
}
}
At the end of the constructor both _availableLanguages and _selectedLanguage variables are set as expected, the combobox's pulldown list contains all items in _availableLanguages but the selected value is not displayed in the combobox. Selecting an item from the pulldown correctly displays it and sets the SelectedLanguage property in the viewmodel. A breakpoint in the setter reveals that _selectedLanguage still contains what it was initialized to until it is overwritten with value.
I suspect that there is some little thing I'm missing, but after trying various things and much googling I'm still stumped. I could achieve the desired result in other ways but really want to get a handle on the proper use of databinding.
You need to change the order of you bindings in XAML so that your ItemsSource binds before the SelectedItem.
<ComboBox x:Name="LanguageSelector" Width="100"
ItemsSource="{Binding AvailableLanguages}"
SelectedItem="{Binding SelectedLanguage,Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If you set a breakpoint on the 'get' of both the SeletedLanguage and AvailibleLanguage, you will notice that the SelectedLanguage gets hit before your AvailibleLanguage. Since that's happening, it's unable to set the SelectedLanguage because the ItemsSource is not yet populated. Changing the order of the bindings in your XAML will make the AvailibleLanguages get hit first, then the SelectedLanguage. This should solve your problem.
1) When you assign the SelectedLanguage, use the public property SelectedLanguage instead of the private _selectedLanguage, so that the setter gets executed,
2) You need to move the assignment of the selectedlanguage to the moment that the view has been loaded. You can do it by implementing the Loaded event handler on the View. If you want to be "mvvm compliant" then you should use a Blend behavior that will map UI loaded event to a viewmodel command implementation in which you would set the selected language.

WPF databinding update comboxbox2 based on selection change in combobox1 with MVVM

I have a combo box that I have bound to a list that exists in my viewmodel. Now when a users makes a selection in that combo box I want a second combo box to update its content.
So, for example, combobox1 is States and combobox2 should contain only the Zipcodes of that state.
But in my case I don't have a predefined lists before hand for combobox2, I need to go fetch from a db.
Also, if needed, I could get all the potential values for combobox2 (for each combobox1 value) before hand, but I'd like to avoiding that if I can.
How do I implement in WPF and using MVVM? I'm fairly new to this whole wpf\databinding\mvvm world.
Something like the following. Note that the code is drastically simplified for the sake of example. In reality, your ViewModel would implement INotifyPropertyChanged and raise PropertyChanged events when the properties were modified.
The key though is the setter of SelectedState. Your ComboBox would bind its SelectedValue property to the ViewModel's SelectedState property. When the property changed, the ZipCodes collection gets re-loaded which another combobox would be bound to.
class MyViewModel {
public ObservableCollection<string> States {
get;
private set;
}
public ObservableCollection<string> ZipCodes {
get;
private set;
}
public string SelectedState {
get { return _selectedState; }
set {
_selectedState = value;
LoadZipCodes(_selectedState);
}
}
public string SelectedZipCode {
get;
set;
}
void LoadZipCodes(string state) {
// repopulate the ZipCodes property
}
}
Another solution. The approximate model:
class StateViewModel
{
public string StateName
{
get {...}
set {...}
}
public ObservableCollection<ZipCodeViewModel> ZipCodes
{
get {...}
set {...}
}
}
class ZipCodeViewModel
{
public string ZipCodeName
{
get {...}
set {...}
}
}
class MainViewModel
{
public ObservableCollection<StateViewModel> States
{
get {...}
set {...}
}
}
And XAML:
<ComboBox ItemsSource="{Binding Path=States}" IsSynchronizedWithCurrentItem="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=StateName}"></Label>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ContentControl Content="{Binding Path=States}">
<ContentControl.ContentTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=ZipCodes}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=ZipCodeName}"></Label>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>

Resources