wpf 事件处理程序绑定样式

发布于 2024-12-09 19:15:04 字数 1489 浏览 0 评论 0原文

我有一个样式,我想使用 RelativeSource 将命令绑定到 EventSetterHandler。该命令位于 viewModel 中。

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="{Binding TextBlockMouseLeftButtonDownCommand, 
                           RelativeSource={RelativeSource Self}}"/>
</Style>

问题是我收到一个错误,因为这有问题(也许不可能以如此简单的方式做到这一点)

我之前用谷歌搜索了很多,我找到了 AttachedCommandBehaviour,但是我认为这与风格不相符。

您能给一些关于如何解决这个问题的提示吗?

更新 13/10/2011

我在 MVVM Light Toolkit EventToCommand 示例程序中发现了这一点:

        <Button Background="{Binding Brushes.Brush1}"
            Margin="10"
            Style="{StaticResource ButtonStyle}"
            Content="Simple Command"
            Grid.Row="1"
            ToolTipService.ToolTip="Click to activate command">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding SimpleCommand}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeave">
                <cmd:EventToCommand Command="{Binding ResetCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

但是在这里,绑定不在样式中。如何将此 EventToCommand 放入按钮的样式?

I have a style, and I want to bind a command to the EventSetter's Handler with RelativeSource. The command is in the viewModel.

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="{Binding TextBlockMouseLeftButtonDownCommand, 
                           RelativeSource={RelativeSource Self}}"/>
</Style>

The problem is that I get an error, because something is wrong with this (maybe it's not possible to do this in such easy way)

I've googled a lot before, and I found the AttachedCommandBehaviour, but I think it doesn't work with style.

Could you give some hints on how to solve this problem?

Update 13/10/2011

I found this in the MVVM Light Toolkit EventToCommand example program:

        <Button Background="{Binding Brushes.Brush1}"
            Margin="10"
            Style="{StaticResource ButtonStyle}"
            Content="Simple Command"
            Grid.Row="1"
            ToolTipService.ToolTip="Click to activate command">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding SimpleCommand}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeave">
                <cmd:EventToCommand Command="{Binding ResetCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

But here, the binding isn't in the style. How can I put this EventToCommand to the style of the button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-12-16 19:15:04

现在,您正在将 MouseLeftButtonDown 事件绑定到 TextBlock.TextBlockMouseLeftButtonDownCommandTextBlockMouseLeftButtonDownCommand 不是 TextBlock 的有效属性,听起来也不像是一个事件处理程序。

我一直使用 AttachedCommandBehavior 样式进行连接对事件的命令。语法通常如下所示(请注意命令绑定中的 DataContext):

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="local:CommandBehavior.Event" Value="MouseLeftButtonDown" />
    <Setter Property="local:CommandBehavior.Command"
            Value="{Binding DataContext.TextBlockMouseLeftButtonDownCommand, 
                            RelativeSource={RelativeSource Self}}" />
</Style>

另一种方法是将 EventSetter 挂接到代码隐藏中的事件,并从那里处理命令:

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="TextBlockMouseLeftButtonDown"/>
</Style>

事件处理程序代码后面...

void TextBlockMouseLeftButtonDown(object sender, MouseEventArgs e)
{
    var tb = sender as TextBlock;
    if (tb != null)
    {
        MyViewModel vm = tb.DataContext as MyViewModel;

        if (vm != null && TextBlockMouseLeftButtonDownCommand != null
            && TextBlockMouseLeftButtonDownCommand.CanExecute(null))
        {
            vm.TextBlockMouseLeftButtonDownCommand.Execute(null)
        }
    }
}

Right now you are binding the MouseLeftButtonDown Event to TextBlock.TextBlockMouseLeftButtonDownCommand. TextBlockMouseLeftButtonDownCommand is not a valid property for a TextBlock, nor does it sound like it's an Event Handler.

I use the AttachedCommandBehavior all the time in styles for hooking up a Command to an Event. The syntax usually looks like this (note the DataContextin the Command Binding):

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="local:CommandBehavior.Event" Value="MouseLeftButtonDown" />
    <Setter Property="local:CommandBehavior.Command"
            Value="{Binding DataContext.TextBlockMouseLeftButtonDownCommand, 
                            RelativeSource={RelativeSource Self}}" />
</Style>

The alternative is to hook the EventSetter up to an event in the code-behind, and process the command from there:

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="TextBlockMouseLeftButtonDown"/>
</Style>

Event handler in code behind...

void TextBlockMouseLeftButtonDown(object sender, MouseEventArgs e)
{
    var tb = sender as TextBlock;
    if (tb != null)
    {
        MyViewModel vm = tb.DataContext as MyViewModel;

        if (vm != null && TextBlockMouseLeftButtonDownCommand != null
            && TextBlockMouseLeftButtonDownCommand.CanExecute(null))
        {
            vm.TextBlockMouseLeftButtonDownCommand.Execute(null)
        }
    }
}
甜心 2024-12-16 19:15:04

由于您正在使用 MVVM,我建议您 Galasoft MVVM Light Toolkit EventToCommand

As you are using MVVM, I suggest you Galasoft MVVM Light Toolkit EventToCommand

奈何桥上唱咆哮 2024-12-16 19:15:04

我对这个问题的回答< /a> 无需任何外部工具包/库即可完成此操作。但是,它不使用 RelativeSource,并且它不是 100% MVVM。它需要代码隐藏事件处理程序中的一行代码。

My answer on this question does the trick without any external tool kits/libraries. However, it does not use RelativeSource, and it is not 100% MVVM. It requires one line of code in a code-behind event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文