Databind to DataTemplate.IsSelected to View Model
How do I databind to the D开发者_Python百科ataTemplate
's IsSelected
property to my view model?
I suppose your DataTemplate
is within an ItemsControl
or a control that derives from it, like ListBox
, DataGrid
etc. In that case you bind IsSelected
in the ItemContainerStyle
Example for ListBox
<ListBox ItemsSource="{Binding MyCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding Selected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<!-- ... -->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
精彩评论