C# WPF中的绑定验证和保存命令

C# WPF中的绑定验证和保存命令,c#,wpf,validation,xaml,data-binding,C#,Wpf,Validation,Xaml,Data Binding,我有几个TextBox绑定到属性。 这些属性由Validation.ErrorTemplate进行测试。 我不使用MVVM 我添加了一个按钮以保存我的输入: <Button Template="{StaticResource BoutonRessourcesTpl}" Command="Save"> <Button.CommandBindings> <CommandBinding Command="Save" Executed="Save_Execut

我有几个
TextBox
绑定到属性。 这些属性由Validation.ErrorTemplate进行测试。 我不使用MVVM

我添加了一个按钮以保存我的输入:

<Button Template="{StaticResource BoutonRessourcesTpl}" Command="Save">
  <Button.CommandBindings>
    <CommandBinding Command="Save"  Executed="Save_Executed" CanExecute="Save_CanExecute"/>
  </Button.CommandBindings>
  <Image Source= "Toolbar_Valider.png" Height="16"/>
</Button>

在我的代码隐藏中,我写了以下内容:

    private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
    {
    }

    private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = IsValid(sender as DependencyObject);
    }

    private bool IsValid(DependencyObject obj)
    {
        // The dependency object is valid if it has no errors, 
        //and all of its children (that are dependency objects) are error-free.
        return !Validation.GetHasError(obj) &&
            LogicalTreeHelper.GetChildren(obj)
            .OfType<DependencyObject>()
            .All(child => IsValid(child));
    }
private void Save_已执行(对象发送方,已执行路由目标)
{
}
私有void Save_CanExecute(对象发送方,canexecuterouteEventArgs e)
{
e、 CanExecute=IsValid(发送方作为DependencyObject);
}
私有布尔值有效(DependencyObject对象)
{
//如果依赖项对象没有错误,则该对象有效,
//它的所有子对象(即依赖对象)都是无错误的。
return!Validation.GetHasError(obj)&&
LogicalTreeHelper.GetChildren(obj)
第()类
.All(child=>IsValid(child));
}

我的问题是,我不知道在哪里调用我的代码来保存输入。

您将代码放在Save_执行的函数中。有关示例,请参见:。尽管在该示例中,CommandBinding被放置在按钮的所有者窗口上

谢谢你的回答!我试图在Save_Executed功能中放入MessageBox,但MessageBox从未出现!此外,isValid返回true!那么,为什么执行的Save_没有被执行?您是否尝试将CommandBinding放入包含按钮的窗口中,如示例中所示?