gpt4 book ai didi

c# - 将 IsMouseOver 绑定(bind)到 UserControl 中的 ViewModel

转载 作者:行者123 更新时间:2023-11-30 13:06:06 26 4
gpt4 key购买 nike

我正在创建一个自定义菜单控件,该控件在使用 MVVM 选择或悬停时更改图像(.PNG 文件)。现在,当我选择一个菜单项时,图像会发生变化,但我正在努力让悬停部分正常工作。这是我将 ViewModel 分配给 View 的代码:

<DataTemplate DataType="{x:Type viewModel:MyMenuItem}">
<view:MyMenuItemControl/>
</DataTemplate>

这是我的 UserControl 代码:

<Grid>
<Image Source="{Binding DisplayImage}">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding LeftClickCommand}"/>
</Image.InputBindings>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsMouseOver" Value="{Binding Path=IsHovered, Mode=OneWayToSource}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>

这不会编译,因为 IsMouseOver 属性是只读的,但我无法弄清楚发生此触发器时如何更新 ViewModel。我是 WPF 和 MVVM 的新手,所以如果这有点无知,我深表歉意。我搜索了大约 2 个小时,但未能找到我可以理解并继续前进的答案。

编辑:这是一个似乎也不起作用的选项 2。当我将鼠标悬停在该项目上时,这会编译但不会更新图像:

<Grid>
<Image Source="{Binding DisplayImage}">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding LeftClickCommand}"/>
</Image.InputBindings>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{Binding DisplayHoverImage}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>

我要采用的解决方案:

<Grid>
<Image>
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding LeftClickCommand}"/>
</Image.InputBindings>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding DisplayImage}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{Binding DisplayHoverImage}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Grid>

最佳答案

首先,定义一个新控件,派生自您尝试使用的控件:

public class HoverOverImage : Image
{
public static DependencyProperty MouseOverFrameAreaProperty = DependencyProperty.Register("MouseOverFrameArea", typeof(bool), typeof(HoverOverImage)
, new FrameworkPropertyMetadata(false, OnMouseOverFrameAreaChanged));
private bool _mouseOverFrameArea = false;

public bool MouseOverFrameArea
{
get => _mouseOverFrameArea;
set
{
_mouseOverFrameArea = value;
if (DataContext != null && DataContext.GetType()==typeof(MyViewModel))
((MyViewModel)DataContext).MouseOverFrameArea = value;
}
}
private static void OnMouseOverFrameAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((HoverOverImage)d).MouseOverFrameArea = (bool)e.NewValue;
}
}

然后在 XAML 中你应该添加:

<local:HoverOverImage x:Name="framesourceImage" RenderOptions.BitmapScalingMode="NearestNeighbor" Source="{Binding CurrentFrame}">
<local:HoverOverImage.Style>
<Style TargetType="{x:Type local:HoverOverImage}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="MouseOverFrameArea" Value="True"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="MouseOverFrameArea" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</local:HoverOverImage.Style>

关于c# - 将 IsMouseOver 绑定(bind)到 UserControl 中的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50428772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com