SelectedItem of SelectedItem
first of all I would like to thank you for the many good posts that i read in this forum. Unluckily I could not find anything of help for my current problem (either here or anywhere else).
What I'm trying to do sounds quite simple, but I have no clue how t开发者_如何学Goo get it to work ... perhaps I'm still to new to wpf or I don't thing wpfy enough :)
I'm designing a front end for a part in an automated manufacturing:
- I have a quantity of places where pallets can be put (but it can be empty as well).
- Each pallet has up to 3 places where parts can be mounted
- Everything is created dynamically of a database and is reacting to changes.
- The position of the parts on the pallet comes from the database as well and should be visualized
What I would like to have is:
- An overview over the pallet-places with a preview of the pallet
- When I select a place I want to see a detail view of the place
- When I click on a part on the pallet of the detailed pallet I want to see details to the part
The first two points are quite simple and work nicely:
- I have got a DataTemplate for every component (part, pallet, pallet-place). Actually those are UserControls that are imported as Datatemplates
- the overview is a ListBox with the places as DataContext
- for the detail-place-view I use the UserControl and bound it to the SelectedItem of the Listbox
I tried to bind the Text of a Textblock to the ID of the selected Part ... and fail.
Probably I could use some global variables in the code behind - but that sound very ugly.
Can anybody help?
I have got a solution ... it is not nice but works.
- I created an event in the pallet, that triggers, when the selected part-place changes
- I handle the event in the pallet-place and create a new one
- And finally I handle it in the overview and change the detailview accordingly
Most likely there are much nicer solutions, but it will suffice.
Perhaps try an ElementName binding?
<TextBlock Text="{Binding ElementName=Name_of_your_Listbox, Path=SelectedItem.ID" />
Can you post a bit more code of your TextBlock and your Binding?
Context is important, if i use a ContentControl and bind its content to the SelectedItem
like this:
<ContentControl Content="{Binding SelectedItem, ElementName=mylistbox}">
I can bind to the ID of the selected item in the DataTemplate like this:
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}" />
</DataTemplate>
</ContentControl.ContentTemplate>
That is because setting the Content
of the ContentControl
automatically sets the DataContext as well, and this binding is relative to the DataContext
since no source (ElementName
, RelativeSource
, Source
) has been specified.
I do not know how your UserControl
handles the context, if the DataContext
is not affected such bindings will not work. You would need to bind directly then:
<uc:MyDetailsView Data="{Binding SelectedItem, ElementName=mylistbox}">
<!-- ... -->
<TextBlock Text="{Binding SelectedItem.ID, ElementName=mylistbox}" />
This of course defeats the purpose of having the binding on the UserControl itself in the first place. But unless you post some relevant code it's quite hard to tell what is wrong.
Also check the Output window in VisualStudio, binding errors will show up there and might provide valuable information as to what went wrong.
精彩评论