Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpf事件设置程序处理程序绑定样式为_C#_Wpf_Binding - Fatal编程技术网

C# wpf事件设置程序处理程序绑定样式为

C# wpf事件设置程序处理程序绑定样式为,c#,wpf,binding,C#,Wpf,Binding,我有一个样式,我想用相对资源将命令绑定到事件设置程序的处理程序。该命令位于viewModel中 <Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}"> <EventSetter Event="MouseLeftButtonDown" Handler="{Binding TextBlockMouseLeftButtonDownComman

我有一个样式,我想用
相对资源
将命令绑定到
事件设置程序
处理程序
。该命令位于viewModel中

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

但在这里,绑定不符合样式。如何将此
EventToCommand
设置为按钮样式?

当您使用MVVM时,我建议您现在将
MouseLeftButtonDown
事件绑定到
TextBlock.textblockmouseleftbuttondown命令
TextBlockMouseLeftButtonDownCommand
不是TextBlock的有效属性,听起来也不像是事件处理程序

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


on在没有任何外部工具包/库的情况下完成了这项工作。但是,它不使用相对资源,也不是100%MVVM。它需要在代码隐藏事件处理程序中包含一行代码。

我看到了带有矩形和EventRigers的示例,并且它也在使用EventSetter?将命令执行放到事件中是个好主意,但我明天将尝试AttachCommandBehavior!谢谢你的回答!AttachCommandBehavior工作得很好。有一件事我不知道该怎么解决。我不仅想为一个事件绑定命令,而且我还找到了这种解决方案:这真的很好,但我如何才能将此CommandBehaviorCollection应用于样式?提前感谢您的回答@ZoltaánBarna它似乎不符合风格或模板Shello Rachel,非常感谢。我遇到了与OP完全相同的问题,在System.Windows.Interactive失败的地方,AttachedCommand行为可以完美地工作。
        <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>
<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>
<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)
        }
    }
}