How do I bind to different property on ComboBox SelectedItem? - wpf

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.

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}}" />

WPF DataGrid ItemsSource - Change from ComboBox

Here is a snippet of XAML:
<ComboBox ItemsSource="{Binding UnileverDataSet.Tables, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Margin="5" x:Name="TableNameComboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TableName}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding UnileverDataSet.Tables[TableNameComboBox.SelectedIndex]}"
UnileverDataSet is a DataSet made up of about 12 DataTables
The idea here is that when the ComboBox value changes, the DataGrid should update based on the index value from the ComboBox.
Is this possible or should I look at another way of doing this?
If I do: UnileverDataSet.Tables[0], then all works and data displays correctly.
You can do element binding with combobox..
Your combox will display list of items in the "UnileverDataSet.Tables" collection. When ever your select a item in Combo box the selected item will bounded to the Datagrid Items source (since we are using element binding)
Here is the sample code
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding SelectedItem,ElementName=TableNameComboBox}">

ComboBox ItemsSource has a different datacontext from SelectedItem

I have ComboBox where I get my ItemsSource from my "Settings" object with the UserTypes ObservableCollection inside, and I need to bind its SelectedItem to my "Employee" object's type property. There are other fields within the window, which binds properly to other properties in the "Employee" object, the ItemsSource is correctly populated with the UserTypes property within Settings, and the only thing that is not working is the type property not binding with the combobo'x selecteditem, possibly because it's datacontext is set to the "Settings" object. Here is what I have so far:
<CollectionViewSource x:Key="settingsViewSource" d:DesignSource="{d:DesignInstance my:Settings, CreateList=True}" />
<CollectionViewSource x:Key="settingsUserTypesViewSource" Source="{Binding Path=UserTypes, Source={StaticResource settingsViewSource}}" />
<ComboBox DataContext="{StaticResource settingsUserTypesViewSource}" Name="userTypeBox" VerticalAlignment="Top" Width="100" Margin="2" ItemsSource="{Binding}"
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
In the code behind, I have:
InitializeComponent();
this.emp = emp;
InfoTab.DataContext = emp;
userTypeBox.DataContext = MainWindow.ViewSettings.UserTypes;
emp is a specific employee bound to the fields correctly, only the combo boxes' selecteditem is not working. Any help would be greatly appreciated.
I don't know if you have a typo in the actual code or if it's just here, but you are missing a bracket
SelectedItem="Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
should be
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

ComboBox Selected Item in 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.

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