将ListBoxItem的IsSelected属性传递给DataTemplate中的UserControl

时间:2013-12-15 01:22:34

标签: c# wpf xaml windows-phone-8 user-controls

我在WP8应用程序页面中有一个UserControl元素列表。

我试图让UserControl本身在列表中处于选定状态时具有不同的视觉效果。

我在UserControl中创建了一个DependencyProperty(IsHighlighted),现在我试图将ListBoxItem的IsSelected属性传递给UserControl。

<ListBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <userControls:MyControl IsHighlighted="{Binding Path=IsSelected ???}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果在WP8中启用了以下功能,那可能很容易:

{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}

但它没有启用..

尝试了很多变化,但我没有想法。任何人吗?

修改

控制台上出现以下错误:

System.Windows.Data Error: BindingExpression path error: 'IsSelected' property not found on 'System.Windows.Controls.ContentPresenter' 'System.Windows.Controls.ContentPresenter' (HashCode=6915811). BindingExpression: Path='IsSelected' DataItem='System.Windows.Controls.ContentPresenter' (HashCode=6915811); target element is 'WP8.UserControls.MyViewControl' (Name='userControlObj'); target property is 'IsHighlighted' (type 'System.Boolean')

1 个答案:

答案 0 :(得分:0)

超级老线程,但万一其他人看起来像我做了.....

我很开心: “{Binding Path = IsSelected,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type ListBoxItem}}

相关问题