我正在尝试使用以下代码更改我的ContentTemplate
:
<DataTemplate x:Key="SidebarItemStyle" DataType="{x:Type domain:SidebarDataTemplate}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource BluePrimaryBrush}" />
<Image Grid.Row="0" Style="{StaticResource SidebarMenuImageSyle}" Source="{Binding Image}" />
<TextBlock Grid.Row="1" Style="{StaticResource SidebarMenuTextStyle}" Text="{Binding Title}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="SidebarSelectedItemStyle" DataType="{x:Type domain:SidebarDataTemplate}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource BluePrimaryBrush}" />
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource TransparentOverlay1Brush}" />
<Image Grid.Row="0" Style="{StaticResource SidebarMenuImageSyle}" Source="{Binding SelectedImage}" />
<TextBlock Grid.Row="1" Style="{StaticResource SidebarMenuTextStyle}" Text="{Binding Title}" />
</Grid>
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="SidebarContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource SidebarItemStyle}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SidebarSelectedItemStyle}" />
</Trigger>
</Style.Triggers>
</Style>
但是,当我单击该项目时,它会继续选择随机项目或仅选择第一个项目。关键是我想使用其他Image
并将覆盖层添加到所选项目。
答案 0 :(得分:1)
我不确定您的情况如何,但是您的ContentTemplate
从未定义ContentPresenter
有点怀疑。无论如何,我认为还有另一种方法可以实现您的目标。尝试以下XAML:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource BluePrimaryBrush}" />
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="{StaticResource TransparentOverlay1Brush}">
<Rectangle.Style>
<Style TargetType="Rectangle">
<!-- Don't show overlay when not selected -->
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
<!-- Show overlay when selected -->
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<Image Grid.Row="0">
<Image.Style>
<Style TargetType="Image">
<!-- Use Image when not selected -->
<Setter Property="Source" Value="{Binding Image}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
<!-- Use SelectedImage when selected -->
<Setter Property="Source" Value="{Binding SelectedImage}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Grid.Row="1" Text="{Binding Title}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!-- Remove default selection highlighting -->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
您定义一个ItemTemplate
,然后根据是否选择了ListBoxItem
来选择是否使用覆盖和SelectedImage
属性。 ListBoxItem.ControlTemplate
替换为空白的ContentPresenter
,以删除所有默认的突出显示(因为您自己这样做)。
答案 1 :(得分:0)
事实证明,这种奇怪的行为是由this post中的DragMoveBehavior
引起的。
public class DragMoveBehavior : Behavior<Window>
{
protected override void OnAttached()
{
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
}
protected override void OnDetaching()
{
AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
}
private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && sender is Window window)
{
// In maximum window state case, window will return normal state and
// continue moving follow cursor
if (window.WindowState == WindowState.Maximized)
{
window.WindowState = WindowState.Normal;
// 3 or any where you want to set window location after
// return from maximum state
Application.Current.MainWindow.Top = 3;
}
window.DragMove();
}
}
}
Window XAML:
<Window ...
xmlns:h="clr-namespace:A.Namespace.Of.DragMoveBehavior"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<i:Interaction.Behaviors>
<h:DragMoveBehavior />
</i:Interaction.Behaviors>
...
</Window>
通过从Window删除行为,这个奇怪的随机单击问题消失了。感谢redcurry的回答,我还在我的应用程序中使用了您的代码。
更新
我将redcurry标记为该代码的可接受答案,但是如果遇到此问题,您可能需要在尝试我的解决方案之前先尝试使用他的解决方案。