开发者

Databind to DataTemplate.IsSelected to View Model

开发者 https://www.devze.com 2023-04-05 13:36 出处:网络
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, Da

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>
0

精彩评论

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