首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UserControl中的事件触发器

UserControl中的事件触发器
EN

Stack Overflow用户
提问于 2018-06-01 04:41:23
回答 1查看 1K关注 0票数 1

已创建用户控件。控件中有一个字段不支持命令,因此我决定使用事件触发器来调用该命令。但它不起作用。应用程序遵循MVVM模式。告诉我,我做错了什么?

在UserControl中为绑定属性创建DP:

代码语言:javascript
复制
    public ICommand EditorFormula {
        get { return (ICommand)GetValue(EditorFormulaProperty); }
        set { SetValue(EditorFormulaProperty, value); }
    }

    public static readonly DependencyProperty EditorFormulaProperty =
        DependencyProperty.Register("EditorFormula", typeof(ICommand), typeof(FormIndexControl), null);

在my UserControl中创建控件:

代码语言:javascript
复制
<UserControl x:Class="UControls.FormIndexControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:wpfm="clr-namespace:WpfMath.Controls;assembly=WpfMath"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:test"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="200" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <StackPanel>
            <wpfm:FormulaControl Formula="{Binding Path=IndexFormula}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <i:InvokeCommandAction Command="{Binding Path=EditorFormula}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </wpfm:FormulaControl>
        </StackPanel>
    </Grid>
</UserControl>

Binding UserControl XAML:

代码语言:javascript
复制
<uc:FormIndexControl EditorFormula="{Binding DataContext.CommandOpenEditor , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

和create命令。DelegateCommand类是由ICommand接口实现的(它肯定可以工作):

代码语言:javascript
复制
public class test{

    public test(){
        CommandOpenEditor = new DelegateCommand(OpenEditorFormula);
        }

public ICommand CommandOpenEditor { get; set; }

    private void OpenEditorFormula(object obj) {
        // He not invoked
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-01 09:07:02

你检查过你的输出窗口了吗?我确信您将看到命令的绑定错误。因为绑定将尝试在usercontrol的datacontext中查找命令,usercontrol是托管usercontrol的窗口。

此外,您还需要使StackPanel可聚焦,因为面板在默认情况下不可聚焦。

因此,您可以命名控件并在InvokeCommandAction中设置ElementName属性。

以下是更新后的代码。

代码语言:javascript
复制
<UserControl
    x:Class="WpfApp4.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApp4"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="usercontrol1"
    d:DesignHeight="300"
    d:DesignWidth="300"
    mc:Ignorable="d">
    <StackPanel
        Height="50"
        Background="Aqua"
        Focusable="True"
        IsHitTestVisible="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <i:InvokeCommandAction Command="{Binding LeftButtonDownCommand, ElementName=usercontrol1}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <TextBox Text="test" />
    </StackPanel>
</UserControl>

<Window
    x:Class="WpfApp4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
    xmlns:local="clr-namespace:WpfApp4"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">

    <StackPanel
        Focusable="True"
        IsHitTestVisible="True"
        Orientation="Vertical">


        <local:UserControl1 LeftButtonDownCommand="{Binding ButtonCommand}" />
    </StackPanel>
</Window>

public class MainViewModel
    {
        public ICommand ButtonCommand { get; set; }
        public MainViewModel()
        {

            ButtonCommand = new DelegateCommand(() => { MessageBox.Show("Left Clicked"); });
        }
    }

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MainViewModel();
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50632463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档