没有绑定部分是否可以使用验证?问题是我的文本框没有绑定到任何对象,但我仍然想验证它的内容。我目前唯一能找到的方法是:

    <TextBox Grid.Row="0" Grid.Column="1" MaxLength="50" x:Name="textBoxTubeName" Margin="5,5,0,5">
        <TextBox.Text>
            <Binding Path="Name" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <validation:InvalidCharactersRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

但是,它也只能在文本框中工作。文本绑定到某些东西(在本例中,名称属性),我如何在不绑定的情况下进行此操作?

多谢!


根据最不发达国家论坛 目前尚不可能,但已计划( 注意:这是一个古老的帖子 )。然而,我仍然找不到实现它的方法,所以它可能还没有实现。


从代码背后来做很难。基本上,您可以从代码中设置临时绑定,并提出验证错误,当输入具有有效值时,您可以再次删除所有临时绑定内容。

在这里我使用的东西,我认为这是一个很糟糕的练习(但从无到有更好):

    /// <summary>
    /// Marks a textBox control as invalid (via validation error) from code.
    /// </summary>
    /// <param name="textBox">The text box.</param>
    /// <param name="errorContent">Content of the error.</param>
    public static void ValidationMarkInvalid(TextBox textBox, String errorContent)
    {
        DependencyProperty textProp = TextBox.TextProperty;
        if (!BindingOperations.IsDataBound(textBox, textProp))
        {
            if (textBox.DataContext == null)
            {
                textBox.DataContext = new EmptyDataContext();
            }

            Binding b = new Binding("CodeBehind");
            b.FallbackValue = textBox.Text;
            b.ValidatesOnExceptions = true;
            BindingOperations.SetBinding(textBox, textProp, b);
        }

        BindingExpression bindingInError =
            textBox.GetBindingExpression(TextBox.TextProperty);

        var validationError = new ValidationError(
            new EmptyValidationRule(),
            bindingInError,
            errorContent,
            new Exception(errorContent));

        Validation.MarkInvalid(bindingInError, validationError);
    }

    /// <summary>
    /// Clears the validation error from a textBox.
    /// </summary>
    /// <param name="textBox">The text box.</param>
    public static void ValidationClear(TextBox textBox)
    {
        DependencyProperty textProp = TextBox.TextProperty;
        if (BindingOperations.IsDataBound(textBox, textProp))
        {
            String value = textBox.Text;
            Validation.ClearInvalid(textBox.GetBindingExpression(TextBox.TextProperty));
            BindingOperations.ClearBinding(textBox, textProp);
            textBox.Text = value;

            EmptyDataContext ctx = textBox.DataContext as EmptyDataContext;
            if (ctx != null)
            {
                textBox.DataContext = null;
            }
        }
    }

    #region Nested Type: EmptyDataContext
    private sealed class EmptyDataContext : INotifyPropertyChanged
    {
        public Object CodeBehind
        {
            get
            {
                throw new FormatException();
            }
            set
            {
                throw new FormatException();
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
    #endregion

    #region Nested Type: EmptyValidationRule
    private sealed class EmptyValidationRule : ValidationRule
    {
        /// <summary>
        /// When overridden in a derived class, performs validation checks on a value.
        /// </summary>
        /// <param name="value">The value from the binding target to check.</param>
        /// <param name="cultureInfo">The culture to use in this rule.</param>
        /// <returns>
        /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
        /// </returns>
        public override ValidationResult Validate(Object value, CultureInfo cultureInfo)
        {
            return new ValidationResult(false, String.Empty);
        }
    }
    #endregion

现在,从你的暗号来看:

ValidationMarkInvalid(textBox, "Please enter something valid!");

为了确认:

ValidationClear(textBox);

另请注意:如果您不想验证异常,可以从上述方法中删除空数据类。


是的,我想我得自己做密码了。我认为微软的想法是专注于绑定是好的,但是由于这个目的限制验证这种方式,并不是那么好。多谢卢卡斯。

嗨。有关于这个问题的最新消息吗?我也想这么做。