ComboBox Selected Item in WPF - wpf

How do you set the combobox selected item in xaml?
I tried something doing like this:
<ComboBox x:Name="cmbProject"
ItemsSource="{Binding Project}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedItem="{Binding Path=Project,Mode=TwoWay}"
SelectedValue="{Binding Path=Id,Mode=OneWay}"/>
The above code does not work. I don't know where I'm going wrong.

The ItemsSource property should be a Collection, i.e. Projects or ProjectList, I guess... Also, you only need to set the DisplayMemberPath and the SelectedValue:
<ComboBox x:Name="cmbProject" ItemsSource="{Binding Projects}"
DisplayMemberPath="Name"
SelectedValue="{Binding Project, Mode=TwoWay}" />
Update: based on the info in the comments the code becomes:
<ComboBox x:Name="cmbProjectStatus"
ItemsSource="{Binding ProjectStatuses}"
DisplayMemberPath="Name"
SelectedValuePath="ID"
SelectedValue="{Binding Path=ProjectStatus.ID}"
SelectedItem="{Binding Path=ProjectStatus}" />
The DataContext has a ProjectStatus property of type ProjectStatus and a ProjectStatuses property of type ObservableCollection<ProjectStatus>.

Have you tried to set IsSynchronizedWithCurrentItem="True" for the combobox?
This worked for me.

Related

WPF ComboBox IsEnabled from Items Count

Im wanting the XAML of a ComboBox to handle its own IsEnabled property based on whether or not it has Items. If the DataTable in the code behind returns Items, id like the ComboBox to be enabled otherwise if no Items are added, it remains or becomes a disabled control. Is this at all possible?
My current ComboBox setup:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}" />
If you want the ComboBox to be enabled when it has items, and disabled when there are no items, you could just bind IsEnabled to the HasItems property:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}"
IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />

SelectedItem is null in DataGridTemplateColumn with ComboBox

I have a Datagrid and DataGridTemplateColumn which is ComboBox
<DataTemplate x:Key="ComboBoxPackagingType">
<ComboBox SelectedItem="{Binding PackagingType.SelectedItem, Mode=TwoWay}" ItemsSource="{Binding PackagingType.ItemsSource}"/>
</DataTemplate>
...
<DataGridTemplateColumn CellTemplate="{StaticResource ComboBoxPackagingType}"/>
The SelectedItem is never changing the value after selecting an Item from list.
I set the breakpoints on both get and set functions and it is stopping on get function after changing the ItemSource of my DataGrid but never on the set function after selecting an Item from list.
Why?
Try adding UpdateSourceTrigger=PropertyChanged to the binding of your ComboBox's selected item like so:
<DataTemplate x:Key="ComboBoxPackagingType">
<ComboBox SelectedItem="{Binding PackagingType.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding PackagingType.ItemsSource}"/>
</DataTemplate>
This worked for me.

ComboBox.SelectionBoxItemStringFormat not set correctly

I am using a ComboBox displaying floats (in Choices) as percent values:
<ComboBox
ItemStringFormat="P0"
IsEditable="True"
IsReadOnly="True"
SelectedItem="{Binding SelectedObject.PrePlanningStatus, Mode=TwoWay, ValidatesOnDataErrors=True, StringFormat=P0}"
ItemsSource="{Binding Choices}" />
The choices are correctly displayed with the percent symbol. However, the SelectedItem is displayed as normal float value without a percent symbol (thus with wrong format).
ItemTemplate has the same behaviour. It seems that the ComboBox.SelectionBoxItemStringFormat property is not set correctly as this value is null at runtime. However this property is readonly. What have I done wrong?
You can use a DataTemplate to specify how items are displayed in a universal way:
<ComboBox IsEditable="True" IsReadOnly="True" SelectedItem="{Binding SelectedObject.CavernDetails.PrePlanningStatus, Mode=TwoWay, ValidatesOnDataErrors=True}" ItemsSource="{Binding Choices}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StringFormat=P0}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I found out that using a ComboBox without IsEditable="True" IsReadOnly="True" settings the selected item is displayed within the correct format. For me these settings are also fine.

How do I bind to different property on ComboBox SelectedItem?

I have a combobox in WPF like this:
<ComboBox Text="Select Language..." IsEditable="True" IsReadOnly="True"
ItemsSource="{Binding XPath=item/#name, Source={StaticResource Items}}"
SelectedItem="{Binding Path=Test, Mode=OneWayToSource}"/>
Where Items is:
<XmlDataProvider x:Key="Items" Source="/itemlist.xml" XPath="/itemlist"/>
Test is a property of type object on ViewModel that is set as datacontext of a window.
Everything works fine, and my Test property receives XmlNode object, which makes sense.
However, I would like to receive different attribute from that xml for example XPath=item/#value
How do I do that?
Use DisplayMemberPath and SelectedValuePath:
<ComboBox Text="Select Language..." IsEditable="True" IsReadOnly="True"
ItemsSource="{Binding XPath=item, Source={StaticResource Items}}"
DisplayMemberPath="#name"
SelectedValuePath="#id"
SelectedValue="{Binding Path=Test, Mode=OneWayToSource}"/>
The selected item will be the item element, it will display the name attribute, and it will bind the id attribute to Test.

Databinding 2 WPF ComboBoxes to 1 source without being "linked"

I have a master-detail scenario where I have 1 ComboBox listing companies from an ObjectDataSourceProvider. Under that I have 2 ComboBoxes binding to the Contacts property from the current Company object. I need to be able to select a different contact in each ComboBox; however, as soon as I change selection in one list the other list updates to the same contact.
I have tried different settings (OneWay vs. TwoWay) but so far nothing seems to work. Here is an excerpt of my XAML.
<Page.Resources>
<!-- This is a custom class inheriting from ObjectDataProvider -->
<wg:CustomersDataProvider x:Key="CompanyDataList" />
</Page.Resources>
<Grid>
<!--- other layout XAML removed -->
<ComboBox x:Name="Customer" Width="150"
ItemsSource="{Binding Source={StaticResource CompanyDataList},Path=Companies,Mode=OneWay}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=Id, Mode=OneWay}"
VerticalAlignment="Bottom" />
<ComboBox x:Name="PrimaryContact" Width="150"
DataContext="{Binding ElementName=Customer,Path=Items,Mode=OneWay}"
ItemsSource="{Binding Path=Contacts,Mode=OneWay}"
DisplayMemberPath="FullName"
SelectedValuePath="Id"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=Id,Mode=OneWay}" />
<ComboBox x:Name="AdminContact" Width="150"
DataContext="{Binding ElementName=OwnerCustomer,Path=Items,Mode=OneWay}"
ItemsSource="{Binding Path=Contacts,Mode=OneWay}"
DisplayMemberPath="FullName"
SelectedValuePath="Id"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=Id,Mode=OneWay}" />
<!--- other layout XAML removed -->
</Grid>
I thought that creating a CollectionViewSource would be the way to go, but I have not been able to make that work. Is there a simple way to do this so the PrimaryContact and AdminContact aren't linked?
Change your "IsSynchronizedWithCurrentItem" attributes to "False".

Resources