I have read many posts on this subject but I can't wrap my head around it. I have a ComboBox of which the ItemSource is binded to an XmlDataProvider. It looks like this:
<ComboBox ItemsSource="{Binding XPath=List/ListItem}">
I need the SelectedValue to be binded to an XPath property of a selected item of a ListBox. So I have two different datacontexts. The ListBox is binded to the same XmlDataProvider. I tried this:
<ComboBox ItemsSource="{Binding XPath=List/ListItems}" SelectedValue="{Binding ElementName="MyListBox" Path=SelectedItem.MyXPathProperty}">
But it didn't work. In the other posts people referenced to a normal property but I need to reference to an XPath property.
How can I bind the SelectedValue to an XPath property of a different element?
<ListItems>
<MyNode MyXPathProperty="SomeValue">
<MyNode MyXPathProperty="SomeValue">
<MyNode MyXPathProperty="SomeValue">
</ListItems>
Related
Is it possible in XAML to set a binding to a property that contains the property to bind to.
Usual binding:
<ListBox ... ItemsSource="{Binding ListItems}"
xaml gets the list from ListItems
what I'd like
<ListBox ... ItemsSource="{Binding *ListName}"
where the ListName object holds the property name to bind to, e.g. ListItems.
Depending on user actions ListName could be changed.
What I'm hoping to do is to avoid having a bunch of combo-boxes.
I thought it might be similar to RelativeSource
https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.binding.path?view=windowsdesktop-6.0
I have a ComboBox in WPF. The ComboBox is inside of a grid, and the grid's DataContext is bound to the SelectedItem of a ListView. The ItemsSource of the ComboBox is set to a StaticResource, located in the window resources. The ItemsSource does not change. I have tried to use both SelectedValue and SelectedItem but both of them cause the same issue for me. The issue is that when the SelectedItem of the ListView is changed, the ComboBox is actually setting the property value from the PREVIOUSLY selected item, to the property value of the NEWLY selected item. Clearly I am doing something wrong, because I have used comboboxes many times in the past without this issue. I have scoured the web and can't find an answer. The closest, most similar question I found was: Strange behaviour (or bug?) with ComboBox in WPF when changing DataContext and having bound ItemsSource and SelectedItem
But it doesn't seem to have a solution. The solutions listed in comments did not work for me.
I created SelectionChanged events for both the ListView and the ComboBox and set breakpoints at each of them and the property that is being set. The property is actually being set BEFORE either one of those are triggered. So even if I wanted to create some hack workaround, I couldn't.
For the record, the ComboBox functionality works perfectly fine. When an object is selected in the ListView, I can see the Template name property, as I should, and the list of items is correct. If I manually change the selected item, the property is changed to a new item, just like it should. The problem is that when I change the selected item in the ListView, the "Template" property of the newly selected object is being set to the "Template" property of the previously selected object. So the combobox is changing before anything else.
The xaml for the ListView and ComboBox are below.
<ListView x:Name="my_ListBox" FlowDirection="RightToLeft"
Margin="5" Grid.RowSpan="2" SelectedIndex="0"
ItemsSource="{Binding Source={StaticResource myList}}"
DisplayMemberPath="Name"
SelectionChanged="my_ListBox_SelectionChanged"/>
<Grid DataContext="{Binding ElementName=my_ListBox, Path=SelectedItem}">
<ComboBox Name="comboBox_myTemplate"
ItemsSource="{Binding Source={StaticResource myTemplatesList}}"
SelectedValue="{Binding Template}"
SelectionChanged="comboBox_myTemplate_SelectionChanged"
DisplayMemberPath="Name" FontSize="20" Margin="5"/>
</Grid>
If I set "IsSynchronizedWithCurrentItem="True"" in the ComboBox the problem is resolved. If someone wants to explain what exactly that is doing and how it works I'd love to hear it. Thanks.
I have a DataGrid and one of the columns is a template column which contains DropDownMenu from MahApps Metro. I successfully populate ItemsSource and items are displayed when menu is clicked.
I'm having problem now binding this selected value to datagrid items source.
I've tried so far:
<Controls:DropDownButton ItemsSource="{Binding ResTypes, RelativeSource={RelativeSource AncestorType=Window}}" Content="{Binding Type, RelativeSource={RelativeSource AncestorType=DataGrid}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
And whenever I select an item, the value doesn't even change on DropDownMenu, like it item can't even be selected.
How could I solve this? Thanks
The DropDownButton has no concept of selection. You should use a SplitButton and bind its SelectedItem property to your source property.
The built-in DataGrid has no Type property though so it's unclear to what property you are trying to bind. Make sure that the type of the items in the ResTypes collection is the same as the Type property that you are trying to bind to.
I know there is a few topics on this problem but haven't find any solutions to this problem I have..
I have a ViewModel with an observable collection and I want to bind this collection to a combo box. However, there is no selected item, no index, just the collection itself.
in the XAML I have
ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="OSCollection.Name"
I believe the trouble lies with the bold above, I want to get a property from the collection called name, but like I say - no item will be selected before the bind.
I could use a foreach or something to extract the properties from the collection but I don't think this is the MVVM and WPF way.
Any help would be grateful.
Thanks
DisplayMemberPath specifies the path to the display property.So it should be Name not OSCollection.Name
ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="Name"
In addition to Sajeetharans comment:
When binding to a List of Type T, DisplayMemberPath will always refer to the Name of a Property of T. In your case it is only "Name"
I have a combobox that looks like this:
<ComboBox x:Name="cbGender"
DisplayMemberPath="Value"
ItemsSource="{x:Static patientObjects:Gender.GenderList}"
SelectedIndex="0"
SelectedItem="{Binding Encounter.Patient.Gender,
Mode=TwoWay}"
SelectedValuePath="Id"
StaysOpenOnEdit="True" />
This all works fine for normal use. However, when I load Encounter.Patient.Gender from my database, then clear the value out. The next use of the combobox has overwritten the the top item in my Gender.GenderList with the value loaded from the database. (In my repro example, Unknown is overwritten with Male).
My GenderList is an ObserverableCollection of static gender properties. Each of those properties is backed by a static readonly gender field.
Because the backing fields are readonly, my guess is that the collection is having a reference updated somehow. But I can't figure out how it could be done with this binding...
Any guesses?
Update: I tried the following with no effect:
Removing SelectedIndex and SelectedValuePath
Adding an event to the GenderList collection's collection changed (did not fire).
Removing the TwoWay mode off the SelectedItem
Changing the Gender.GenderList to be a ReadOnlyObserveableCollection.