gpt4 book ai didi

wpf - 带有附加事件的 EventToCommand

转载 作者:行者123 更新时间:2023-12-01 01:07:42 26 4
gpt4 key购买 nike

我在附加事件中使用 Validation.Error的文本框。

Validation.Error

我想绑定(bind)到EventToCommand .

通常它不起作用:

 <TextBox Height="20" Width="150" Text="{Binding MyProperty,NotifyOnValidationError=True,ValidatesOnDataErrors=True}" ><!--Validation.Error="TextBox_Error"-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Validation.Error">
<mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" ></mvvm:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>

所以我找到了一种方法,你可以在下面的链接中看到它:

Attached an mvvm event to command to an attached event

但我收到一个错误:
RoutedEventConverter cannot convert from System.String.

enter image description here

任何人都可以帮忙吗?

编辑:

我在 ViewModel 中的命令
  public MyViewModel()
{
MyCmd = new RelayCommand<RoutedEventArgs>(Valid);
}

public RelayCommand<RoutedEventArgs> MyCmd { get; set; }

private void Valid(RoutedEventArgs args)
{
//Do something
}

最佳答案

根据您发布的链接,

类(class)RoutedEventTrigger预计 RoutedEvent并且您的 xaml 无法转换字符串 Validation.Error到所需的类型。

所以切换

<i:Interaction.Triggers>
<view_model:RoutedEventTrigger RoutedEvent="Validation.Error">
<mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" />
</view_model:RoutedEventTrigger>
</i:Interaction.Triggers>


<i:Interaction.Triggers>
<view_model:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
<mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" />
</view_model:RoutedEventTrigger>
</i:Interaction.Triggers>

应该没问题

关于wpf - 带有附加事件的 EventToCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com