Que representa un error de validación dentro de un control que no es del control enlazado a datos?

StackOverflow https://stackoverflow.com/questions/4081550

Pregunta

Tengo un TextBlock dentro de un DockPanel dentro de un Border. El TextBlock es enlazado a datos y se pueden cambiar con un teclado en pantalla. Por lo tanto, he enganchado hasta la validación y funciona maravillosamente. Mi dilema es que sólo puedo parecen cambiar el estilo del TextBlock cuando hay un error de validación. Quiero desencadenar el estilo del Border. ¿Cómo puedo hacer esto?

También hay una manera de vincular a la ErrorContents en otra parte de la vista?

¿Fue útil?

Solución

Sólo se una a su cuadro de texto de algún otro control. A continuación he obligado propiedad contenido de una etiqueta al de un cuadro de texto (Validation.Errors) [0] propiedad .ErrorContent.

<DockPanel>

    <!-- TextBox bound to a data object-->
    <TextBox Name="textBox1" DockPanel.Dock="Top" Text="{Binding Path=Age, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>

    <!-- A label that displays the errors from textBox1 -->
    <Label Content="{Binding ElementName=textBox1, Path=(Validation.Errors)[0].ErrorContent}"></Label>

</DockPanel>

Otros consejos

Creo que esto va a resolver su problema:

Xaml:

<Window x:Class="WpfErrorsBorderColor.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfErrorsBorderColor="clr-namespace:WpfErrorsBorderColor" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="{x:Type WpfErrorsBorderColor:MyControl}" TargetType="{x:Type WpfErrorsBorderColor:MyControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="WpfErrorsBorderColor:MyControl"  >
                    <Border Name="bd" >
                        <DockPanel LastChildFill="True"  >
                            <TextBox Text="{Binding Path=Description,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top"/>
                            <TextBlock Name="textBlock" Text="{Binding Path=Description,ValidatesOnDataErrors=True,NotifyOnValidationError=True}"></TextBlock>
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger SourceName="textBlock" Property="Validation.HasError" Value="True">
                            <Setter TargetName="bd" Property="BorderBrush" Value="Red" />
                            <Setter TargetName="bd" Property="BorderThickness" Value="4" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<WpfErrorsBorderColor:MyControl></WpfErrorsBorderColor:MyControl>

Código:

    using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace WpfErrorsBorderColor
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Data data = new Data() { Description = "123" };

            DataContext = data;
        }
    }

    public class Data : IDataErrorInfo, INotifyPropertyChanged
    {
        private string description;
        public string Description
        {
            get { return description; }
            set
            {
                if (description != value)
                {
                    description = value;

                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("Description"));
                }
            }
        }

        string CheckLenght(string str)
        {
            return (str.Length < 1 || str.Length > 10) ? "Strign must be between 1 and 10 letters" : String.Empty;
        }


        public string Error
        {
            get { return CheckLenght(Description); }
        }

        public string this[string columnName]
        {
            get { return CheckLenght(Description); }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class MyControl:UserControl
    {
        public MyControl()
        {

        }
    }
}

ErrorContents puede enlazar la misma manera;)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top