开发者

Set the Selected Item from a DataTemplate

I have a graph displaying a number of view models. The look of these viewmodels in the graph is governed by a set of DataTemplates.

I want to do a Master-Detail view so that when one of the View Models in the graph is selected, the details are shown on another portion of the UI. The issue I'm having is grabbing the mouse click from the graph.

I could add a property to my graph called "SelectedItem" and then bind the details view to it but I don't know how to set the SelectedItem bases on a click on a DataTemplate.

A typical DataTemplate shown in the graph:

<!-- The master control -->
<Grid>
    <Grid.Resources>
         <DataTemplate DataType="{x:Type prove:JobVM}">
              <!-- the view of this type in the Graph -->
         </DataTemplate>
    </Grid.Resources>

    <prove:CustomGraph Graph={Binding JobGraph}/>  
</Grid>                         

<!-- The detail control -->
<ContentControl  Content="HOW DO I SET CONTENT TO BE THE SELECTED ITEM IN THE GRAPH?" >
    <ContentControl.Resources>
         <DataTemplate DataType="{x:Type prove:JobV开发者_StackOverflowM}">
             <!-- the detail view -->
         </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

Any ideas?


You can add a MouseLeftButtonDown event handler on the graph and have the handler check for the element via some Tag, you can then get the datacontext and update a property accordingly on your view's viewmodel. You can then bind the details section of your view to this property.

Hope this helps :)


You should be able to get the SelectedItem via the RelativeSource.TemplatedParent property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜