将DataTemplate.IsSelected数据绑定到视图模型

3

我如何将数据绑定到我的视图模型的的IsSelected属性?


DataTemplate 没有 IsSelected 属性。请提供更多细节。 - Miklós Balogh
1个回答

5
我假设您的位于或继承自它的控件中,例如、等。在这种情况下,您需要在中绑定属性。
以为例:
<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>

“Selected” 在我的绑定到列表框的每个视图模型集合中。 - Daniel A. White
1
那么这应该可以正常工作,你试过了吗?每个DataTemplate都在一个ListBoxItem容器中,而它是具有IsSelected属性的容器。ListBoxItemDataTemplate都具有相同的DataContext - Fredrik Hedblad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接