C# 将ListBoxItem的IsSelected属性传递给DataTemplate内的UserControl
我在WP8应用程序页面中有一个UserControl元素列表 我试图使UserControl本身在列表中处于选定状态时具有不同的视觉效果 我已经在UserControl中创建了一个DependencyProperty IsHighlighted,现在我正试图将ListBoxItem的IsSelected属性传递到UserControl中C# 将ListBoxItem的IsSelected属性传递给DataTemplate内的UserControl,c#,wpf,xaml,windows-phone-8,user-controls,C#,Wpf,Xaml,Windows Phone 8,User Controls,我在WP8应用程序页面中有一个UserControl元素列表 我试图使UserControl本身在列表中处于选定状态时具有不同的视觉效果 我已经在UserControl中创建了一个DependencyProperty IsHighlighted,现在我正试图将ListBoxItem的IsSelected属性传递到UserControl中 <ListBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedIte
<ListBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate >
<userControls:MyControl IsHighlighted="{Binding Path=IsSelected ???}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是它没有被启用
我尝试了很多变化,但我没有主意。有人吗
编辑:
控制台上出现以下错误:
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')
超旧的线程,但万一有人来像我一样 我很高兴:
{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}您尝试过ishighted={TemplateBinding IsSelected}吗?TemplateBinding产生XAML异常。我还尝试过TemplateBinding的完整版本:{Binding IsSelected,RelativeSource={RelativeSource Mode=TemplatedParent}这似乎没有什么影响。你所写的应该是有效的,你有没有尝试过在dp定义中为改变的IsHighlighted设置回调并在那里设置断点?事实上,断点没有被击中。看我的编辑…一种乐趣?对于我们凡人来说,你能详细说明你的答案吗?
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')