WPF eventsetter处理风格结合 [英] wpf eventsetter handler binding in style

查看:1058
本文介绍了WPF eventsetter处理风格结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个风格,我希望将命令绑定到 EventSetter 处理程序的RelativeSource 。该命令在视图模型。

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)

我GOOGLE了之前很多,而且我发现 AttachedCommandBehaviour ,但我认为它不与工作作风。

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?

更新13/10/2011

我发现这在MVVM光工具包 EventToCommand 例子程序:

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>

但在这里,绑定是不是在风格。我怎样才能把这个 EventToCommand 到按钮的样式?

推荐答案

现在你的的MouseLeftButtonDown 事件结合 TextBlock.TextBlockMouseLeftButtonDownCommand TextBlockMouseLeftButtonDownCommand 不是一个TextBlock一个有效的属性,也不听起来像它的事件处理程序。

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.

我使用AttachedCommandBehavior在样式命令挂接到事件所有的时间。语法通常看起来像这样(注意的DataContext 在命令绑定):

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>

另一种方法是向上勾EventSetter在$ C $事件C-后面,并从那里处理命令:

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>

在code

事件处理程序的背后...

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)
        }
    }
}

这篇关于WPF eventsetter处理风格结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆