ComboBox ItemsSource has a different datacontext from SelectedItem - wpf

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

Related

ComboBox doesn't show current value of assigned field inside a ListView

I have a DataTemplate with a Combobox inside a ListView like this
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Dimensions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
DisplayMemberPath="Description"
SelectedValuePath="Id"
SelectedItem="{Binding DimName}"/>
</DataTemplate>
The combobox is populated correctly, but it doesn't select the content according to underlying field (ie. Dimension.DimName).
Here's how the ListView is declared
<ListView
Name="lstCriteria"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Margin="5"
AlternationCount="2"
ItemContainerStyle="{StaticResource CriteriaItemStyle}"
ItemsSource="{Binding Source={StaticResource CriteriaList}}" DockPanel.Dock="Top"
IsSynchronizedWithCurrentItem="True">
If I replace combobox with a TextBlock it does show the DimName Field's value, like this
<TextBox Text="{Binding DimName}"/>
What am I missing ?
Does your DimName come directly from the Dimensions list?
By default, if a ComboBox's Items is set to a custom class, it will compare the SelectedItem to an item in the ItemSource by reference. It will not match the item if they do not refer to the exact same object in memory, even if the object's data is the same.
To get around that you can either set SelectedValue and SelectedValuePath instead of SelectedItem on your ComboBox, or you can overwrite the Equals() method of your DimName class to return true if an object's data is equal
Edit
In regards to your comment below, is DimName a Dimension object? If so then setting SelectedItem should work fine. If it's an long you'll need to set SelectedValue, not SelectedItem. If it's something else, you may need a converter to convert it into a Dimension object
As Rachel suggested, I added a new Property to my class called Dimension of class Dimension like this
public Dimension Dimension
{
get { return _dimension; }
set { _dimension = value; }
}
and then set SelectedItem="{Binding Dimension}" as follows,
<ComboBox ItemsSource="{Binding DataContext.Dimensions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
DisplayMemberPath="Description"
SelectedValuePath="Id"
SelectedItem="{Binding Dimension}">
</ComboBox>
The silly mistake I was making was,
As my combobox was populating correctly, I was hoping that WPF will somehow match the DimName content with one of the items in the ComboBox automatically, which is not possible.

Silverlight data bind combobox items and value separately

I can use databinding to bind the contents of a combobox to a collection, or I can bind the selected value in the combobox to a member of a class, but I can't do both at the same time. I want to be able to bind the contents to one thing and the selected value to something else, I guess the combobox can't handle two datacontexts or I'm not specifying them explicitly. Example below, I'd appreciate any help! Thanks.
In XAML:
<ComboBox Name="Combo" ItemsSource="{Binding}"
SelectedValue="{Binding ID, Mode=TwoWay}"/>
In code:
LayoutRoot.DataContext = myClass;
Combo.DataContext = items;
This should do it for you, or at least be close.
<Grid DataContext="{Binding Source=MyObject}">
<ComboBox x:Name="Combo"
ItemsSource="{Binding Source=MyCollection}"
SelectedValue="{Binding Path=ID, Mode=TwoWay}"/>
</Grid>

WPF DataTemplate and Binding - Is this possible in xaml?

I am DataTemplating a listbox's ItemSource to display a series of comboboxes. I want to give the DisplayMemberPath of the combo to a property, which is in a different source than its own ItemsSource. (Assuming DisplayMemberPath is just a string representing name of a property, I am getting this from the user). I have achieved this with a CollectionViewSource, but all the comboboxes are displaying the same list.
What I am expecting to have after data templating is to have comboboxes display,
ComboboxInstance1.DisplayMemberPath = PropertyMapOfEmployee in FilterControls[0]
ComboboxInstance2.DisplayMemberPath = PropertyMapOfEmployee in FilterControls[1]
Is this possible to achieve in XAML ?
Thanks. Mani
UserControl:
<Resources>
<CollectionViewSource x:Key="bindingSource" Source="{Binding BindingItems}"/>
<CollectionViewSource x:Key="FilterSource" Source="{Binding FilterControls}"/>
<DataTemplate DataType="{x:Type CustomTypes:FilterElement}">
<ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}"
DisplayMemberPath="{Binding Source={StaticResource FilterSource},
Path=PropertyMapofEmployee}" />
</DataTemplate>
<Resources>
---
<DockPanel>
<ListBox x:Name="lstBox" ItemsSource="{Binding FilterControls}" />
</DockPanel>
ViewModel:
List<FilterElement> FilterControls;
List<Employee> Employees
class FilterElement
{
string Caption;
String PropertyMapofEmployee
}
<ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}"
DisplayMemberPath="{Binding PropertyMapofEmployee}" />
I'm not sure you can do that in XAML. (Having the DisplayMemberPath point to a property that is on an object other than the DataContext). You may want to look at the RelativeSource Class to see if that would meet your needs.
Have you thought about providing a reference in your Employee object to the FilterElement and then hooking up to the binding the Employee.PropertyMapOfEmployee property that you've created?

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.

Binding a WPF ComboBox to a different ItemsSource within a ListBox DataTemplate

I have a ListBox that contains a textbox and a combobox in its datatemplate:
<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Name}" />
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to bind the listbox to a collection of objects (which I've done successfully), but I want the combobox in the above datatemplate to have its itemssource set to a local property on the window (array of ints). I still want the combobox to have a two-way bind between its selected item and a property on the collection of objects...
I have the following in code:
PitcherListBox.DataContext = this;
Basically in the end, I want the combobox within the listbox to have a different itemssource than the listbox itself. I can't seem to figure out how to change the ComboBox's ItemsSource in XAML. Can someone provide me some feedback? Thanks!
Try this:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
Note that you need to replace YourWindowTypeHere with the type of the Window containing the LocalArrayOfIntsProperty! Also remember that you will need to define an xml namespace for that type!

Resources