已创建用户控件。控件中有一个字段不支持命令,因此我决定使用事件触发器来调用该命令。但它不起作用。应用程序遵循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);
在my 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>
Binding UserControl XAML:
<uc:FormIndexControl EditorFormula="{Binding DataContext.CommandOpenEditor , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
和create命令。DelegateCommand类是由ICommand接口实现的(它肯定可以工作):
public class test{
public test(){
CommandOpenEditor = new DelegateCommand(OpenEditorFormula);
}
public ICommand CommandOpenEditor { get; set; }
private void OpenEditorFormula(object obj) {
// He not invoked
}
}
发布于 2018-06-01 09:07:02
你检查过你的输出窗口了吗?我确信您将看到命令的绑定错误。因为绑定将尝试在usercontrol的datacontext中查找命令,usercontrol是托管usercontrol的窗口。
此外,您还需要使StackPanel可聚焦,因为面板在默认情况下不可聚焦。
因此,您可以命名控件并在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();
}
}
https://stackoverflow.com/questions/50632463
复制相似问题