相关疑难解决方法(0)

使用WPF验证规则并禁用"保存"按钮

在单击"保存"按钮之前,我有一个页面,其中几个文本框不能为空.

<TextBox...

                <TextBox.Text>
                    <Binding Path ="LastName" UpdateSourceTrigger="PropertyChanged">

                        <Binding.ValidationRules>
                            <local:StringRequiredValidationRule />
                        </Binding.ValidationRules>                              
                    </Binding>
                </TextBox.Text>
Run Code Online (Sandbox Code Playgroud)

我的规则有效.我的文本框周围有一个红色边框,直到我输入一个值.所以现在我想将此验证规则添加到我的其他文本框中.

现在,如何在页面没有错误之前禁用"保存"按钮?我不知道要检查什么是否有任何验证错误.

validation wpf validationrules

28
推荐指数
3
解决办法
5万
查看次数

如果验证失败,请在WPF中禁用"保存"按钮

我采用了似乎是使用IDataErrorInfo接口和样式验证WPF中文本框的标准方法,如下所示.但是,如何在页面无效时禁用"保存"按钮?是通过触发器以某种方式完成的吗?

Default Public ReadOnly Property Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item
    Get
        Dim valid As Boolean = True
        If propertyName = "IncidentCategory" Then
            valid = True
            If Len(IncidentCategory) = 0 Then
                valid = False
            End If
            If Not valid Then
                Return "Incident category is required"
            End If
        End If

        Return Nothing

    End Get
End Property
Run Code Online (Sandbox Code Playgroud)

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="3" />
    <Setter Property="Height" Value="23" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Border BorderBrush="Red" …
Run Code Online (Sandbox Code Playgroud)

validation wpf save

21
推荐指数
1
解决办法
3万
查看次数

绑定路径中的括号是什么意思?

最近我在MSDN上阅读了"数据绑定概述"一文,有这样的示例代码:

<TextBox.ToolTip>
  <Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"/>
</TextBox.ToolTip>
Run Code Online (Sandbox Code Playgroud)

我知道{}表示标记扩展但是这里的括号是什么意思?有人可以分享链接到解释这样的语法.谢谢!

Path="(Validation.Errors)[0].ErrorContent"
Run Code Online (Sandbox Code Playgroud)

data-binding syntax wpf xaml parentheses

9
推荐指数
3
解决办法
2425
查看次数

命令CanExecute为false时,按钮不会被禁用

我有一个简单的窗口,带有一个带有命令的ViewModel按钮.

我希望如果MyCommand.CanExecute()为false,则禁用该按钮.但似乎WPF只会在首次绘制窗口时设置IsEnabled属性.任何后续操作都不会影响按钮的可见状态.我正在使用Prism的DelegateCommand.

我的看法:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Click Here" Command="{Binding MyCommand}" Width="100" Height="50"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

和我的ViewModel:

public class MyVM : NotificationObject
{
    public MyVM()
    {
        _myCommand = new DelegateCommand(DoStuff, CanDoStuff);
    }

    private void DoStuff()
    {
        Console.WriteLine("Command Executed");
        _myCommand.RaiseCanExecuteChanged();
    }

    private bool CanDoStuff()
    {
        var result =  DateTime.Now.Second % 2 == 0;
        Console.WriteLine("CanExecute is {0}", result);
        return result;
    }

    private DelegateCommand _myCommand;

    public ICommand MyCommand
    {
        get
        {
            return _myCommand;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

50%的时间,当我的应用程序加载时,按钮被正确禁用.但是,如果在窗口加载时启用它,并且我单击按钮执行命令,我希望该按钮有50%的时间被禁用,但它永远不会.该命令不会执行,但我仍然可以单击该按钮.当CanExecute()为false时,如何让WPF理解应该禁用该按钮?

wpf command prism mvvm

9
推荐指数
1
解决办法
1万
查看次数