【问题标题】:Eventtrigger in UserControl用户控件中的事件触发器
【发布时间】:2018-06-01 15:21:36
【问题描述】:

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

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

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

在我的 UserControl 中创建控件:

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

绑定用户控件 XAML:

<uc:FormIndexControl EditorFormula="{Binding DataContext.CommandOpenEditor , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>

并创建命令。 DelegateCommand 类实现了 ICommand 接口(确实有效):

public class test{

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

public ICommand CommandOpenEditor { get; set; }

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

【问题讨论】:

  • 尝试调试,看看你的 UC 是否获得了正确的 dataContext

标签: c# wpf


【解决方案1】:

您是否检查了“输出”窗口?我相信您会看到该命令的绑定错误。因为绑定会尝试在用户控件的数据上下文中查找命令,该用户控件是托管用户控件的窗口。

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

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

这是更新后的代码。

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

【讨论】:

  • 你也可以使用相对源。如果不想使用名称
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多