Set selected value of combobox - wpf

I want to set selected value of a combobox
I am receiving a DataTable from the database looking like this:
this Datatable is bound to this combobox.
<ComboBox
DisplayMemberPath="KommuneNavn"
SelectedValuePath="KommuneNr"
ItemsSource="{Binding KommuneNavne}"
SelectedValue="{Binding KommuneNr, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="3"
IsEnabled="{Binding IsUdenlandskAdresse, Converter={StaticResource BooleanNotConverter}}" />
In my viewmodel I have a specific KommuneNr stored in a property. I would like to have my combobox set to show the KommuneNavn that matches with this KommuneNr.
Example:
I have the KommuneNr 101 stored in my viewmodel, the KommuneNavn that matches with this is København I would then like to have my combobox be set to København.
This was pretty difficult to explain, I hope I am making sense. Otherwise feel free to ask.

KommuneNavne must be an ObservableCollection, and in your ViewModel you should implement INotifyPropertyChanged (not described in this example)
<ComboBox
ItemsSource="{Binding KommuneNavne}"
SelectedValue="{Binding KommuneNr, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="KommuneNavn"
SelectedValuePath="KommuneNr"
/>
ViewModel
public class MyViewModel : INotifyPropertyChanged
{
public ObservableCollection<KommuneNavn> KommuneNavne
{
get { value = _kommuneNavne; } //=> _kommuneNavne;
set
{
_kommuneNavne = value;
OnPropertyChanged(nameof(KommuneNavne));
}
}
public long KommuneNr
{
get { value = _kommuneNr; } //=> _kommuneNr;
set
{
if (_kommuneNr == value) return;
_kommuneNr = value;
OnPropertyChanged(nameof(KommuneNr));
}
}
private void SetValue(int valueToSet)
{
KommuneNr = valueToSet;
}
}

Related

WPF Datagrid MVVM : Combobox Binding using DatagridTemplateColumn

Have a WPFDatagrid binded to combobox using Datagridtemplatecolumn. Finding difficult to get the selectedItem of the combobox binding. Have found similar examples around but that's not resolving my problem.
Please find the code snippet of my XAML and the data structure below:
public class X
{
public X ()
{
abc = new ObservableCollection<P>();
}
public ObservableCollection<P> Y
{
get { return abc; }
set { abc = value; PropertyChanged("Y"); }
}
}
public class P
{
private string id;
private string name;
public string ID
{
get
{
return id;
}
set
{
id = value;
InvokePropertyChanged("ID");
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
InvokePropertyChanged("Name");
}
}
}
I have a datastructure defined above that implements the INotifyPropertychanged interface.
<controls:DataGrid Name="datagrid" AutoGenerateColumns="False" ItemsSource="{Binding XList}" Grid.Row="0"
SelectedItem="{Binding SelectedX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Header="Yl">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Y}"
IsSynchronizedWithCurrentItem="False" DisplayMemberPath="Name"
SelectedValue="{Binding Path=SelectedY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"
SelectedValuePath="SelectedY"
SelectedItem="{Binding SelectedY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
Now, in view model, have a observablecollection of List of X i.e., XList and that is binded to the datagrid in the XAML. and have Y within each row of the datagrid binded to the Combobox. Have a property as SelectedY, binded to the SelectedItem of the combobox.
Have also a property as SelectedX binded to the selectedItem of the datagrid, which works fine.
Issue is not able to get the Selected Item binding of the Combobox. Not able to set the selected item for the combobox when the selection has changed.
Can anybody help me out to set the selecteditem binding of the combo box?
Where is set your datacontext ?
You can do something like that :
<controls:UserControl x:Name=MainControl>
<controls:UserControl.DataContext>
<classpath:X/>
</controls:UserControl.DataContext>
<controls:DataGrid ItemsSource={Binding YourItemsContainer}>
<controls:DataGrid.Columns>
<controls:DataGridComboBoxColumn ItemsSource={Binding ElementName=MainControl,Path=DataContext.Y}
SelectedItem={Binding ElementName=MainControl,Path=DataContext.SelectedY}
DisplayMemberPath=Name />
</controls:DataGrid.Columns>
</controls:DataGrid>
</controls:UserControl>
The idea is to set a name to the root element connected to your datacontext, then you can access to it's datacontext property easily by the path. When you are inside of a template, the datacontext is the ItemsSource objects.
Hope it will help you a little !

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.

Binding Combobox column's selectedItem property Silverlight MVVM

I am using below Datagrid, (using MVVM pattern), here what I want is when I select something in the combobox, some kind of notification should happen in the ViewModel saying that this Row’s combobox selectedItem is changed to this value. Right now the notification is happening in the Set method of SelectedEname which is inside class SortedDetails(custom entity) and not a part of viewmodel. Please have a look at the code below and let me know If we can send the notification to videmodel in any way using MVVM pattern.
<c1:C1DataGrid x:Name="datagrid1" ItemsSource="{Binding Path=SortedDetailsList,Mode=TwoWay}" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay}"/>
<c1:DataGridTemplateColumn Header="ENGAGEMENT">
<c1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cmbEngagement" ItemsSource="{Binding EDetails, Mode=TwoWay}" DisplayMemberPath="EName" SelectedItem="{Binding SelectedEName,Mode=TwoWay}">
</ComboBox>
</DataTemplate>
</c1:DataGridTemplateColumn.CellTemplate>
</c1:DataGridTemplateColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
SortedDetailsList is a list of SortedDetails entity, which looks like this :-
public class SortedDetails
{
Private string name;
Private ObservableCollection<details> eDetails;
Private details selectedEname;
public string Name
{
get { return name; }
set { name = value; }
}
public ObservableCollection<details> EDetails
{
get { return eDetails; }
set { eDetails = value; }
}
public details SelectedEname
{
get { return selectedEname; }
set { selectedEname = value; }
}
}
public class Details
{
Private string eName;
Private int eId;
public string EName
{
get { return eName; }
set { eName = value; }
}
public int EId
{
get { return eId; }
set { eId = value; }
}
}
The reason i was asking this question was because i was looking to avoid writing code in codebehind, but in this case not able to avoid the same. So, here is the solution (for me) :-
Add an event delegate or any mediator pattern which will inform the ViewModel that selection is changed from the SelectionChanged event of Combobox...
You can put your ViewModel in the View's resources and bind to the property of ViewModel:
<ComboBox x:Name="cmbEngagement" ItemsSource="{Binding EDetails, Mode=TwoWay}" DisplayMemberPath="EName" SelectedItem="{Binding SelectedEName, Mode=TwoWay, Source={StaticResource ViewModel}">
where SelectedEName is a property of your ViewModel.
You want to use some mechanism for allowing events to invoke commands or verbs (methods) on your view model.
For example, using Actions in Caliburn.Micro, you could write the following:
<ComboBox x:Name="cmbEngagement" ...
cal:Message.Attach="[Event SelectionChanged] = [Action EngagementChanged($dataContext)]>
and in your view model:
public void EngagementChanged(SortedDetails details)
{
// access details.Name here
}
Note that actions in Caliburn.Micro bubble, so it would first look for an EngagementChanged method on SortedDetails type, and then look on your view model.

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 ComboBox databinding ItemsSource item.ToString() to property setter. Why?

For some reason when I bind a ComboBox to a list of POCOs, the property which is bound to SelectedValue is set twice:
With value = POCO.ToString()
With value = POCO.Key property, which is the intended behaviour
I have the following ComboBox that is bound to properties in my ViewModel:
<ComboBox ItemsSource="{Binding Path=AllowedClassifications}"
DisplayMemberPath="Value"
SelectedValue="{Binding Path=TargetGroup.Classification}"
SelectedValuePath="Key" />
The properties in ViewModel are defined as:
//ICollection is implemented by ObservableCollection<T>
//DataBaseFieldValue has two public properties: string Key, string Value
public ICollection<DatabaseFieldValue> AllowedClassifications
{
get { return _allowedClassifications; }
private set { _allowedClassifications = value; }
}
public Model.TargetGroup TargetGroup
{
get { return _targetGroup; }
private set { _targetGroup = value; OnPropertyChanged("TargetGroup"); }
}
TargetGroup.Classification is defined as:
public string Classification
{
get { return _classification; }
set
{
System.Diagnostics.Debug.WriteLine("Classification: " + value);
_classification = value;
OnPropertyChanged("Classification");
}
}
Debug output:
Classification:
MyNamespace.DatabaseFieldValue
Classification: 2
What's happening here? Am I doing this completely wrong?
Everything looks OK in your code, except for the fact that according to your XAML the property which is bound to SelectedValue should be set to the POCO.Key value rather than POCO.Value (as you wrote you expected). I have just created a test project with similar setup and everything works.
Alternatively, you could try using SelectedItem property of combobox in combination with ItemTemplate:
<ComboBox ItemsSource="{Binding Path=AllowedClassifications}"
SelectedItem="{Binding Path=TargetGroup.Classification}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
In this case the TargetGroup.Classification property must be of type DatabaseFieldValue.

Resources