Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Template Design

Tags:

wpf

xaml

It seems like I read another question / answer on this site about this issue but I cannot recall what the answer was and now I cannot find the original post.

I am not a fan of the default error template in WPF. I understand how to change this error template. However, if I add some content to the end of, say, a textbox, the size of the textbox does not change and the added content will (potentially) get clipped. How do I alter the textbox (I believe the correct termonology is adorned element) in this scenario so that nothing gets clipped?

Here is the XAML for the error template:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel>
          <AdornedElementPlaceholder />
          <TextBlock Foreground="Red" Text="Error..." />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Here is the XAML for a couple of textboxes in the form:

<StackPanel>
  <TextBox Text="{Binding...}" />
  <TextBox />
</StackPanel>
like image 634
Jason Richmeier Avatar asked Sep 19 '12 14:09

Jason Richmeier


People also ask

How do I create a 404 error?

One typical trigger for an error 404 message is when the page has been deleted from the website. The page was moved to another URL and the redirection was done incorrectly. You entered an incorrect URL address. Although it happens very rarely, sometimes the server malfunctions.

How do I code a 404 page in HTML?

Step 2: Add the following text to the . htaccess file: “ErrorDocument 404 /404. html”. This line names your 404 error page as '404.

What is Template in HTML and CSS?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript.

How do I create a 404 page in WordPress?

In the left-hand menu of the WordPress Admin Dashboard, go to Appearance -> 404 Error Page. Select the page you have just customized as your 404 page and set it as 404-error page that'll be displayedappear by default, when users land on a broken link: Click Save Changes and that's it.


1 Answers

Here's a solution adapted from Josh Smith's article on Binding to (Validation.Errors)[0] without Creating Debug Spew.

The trick is to define a DataTemplate to render the ValidationError object and then use a ContentPresenterto display the error message. If there is no error, then the ContentPresenter will not be displayed.

Below, I have shared the code of the sample app that I created.

Without errorsWith errors

Here is the XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="WidthAndHeight"
        Title="MainWindow">
    <StackPanel Margin="5">
        <StackPanel.Resources>
            <DataTemplate DataType="{x:Type ValidationError}">
                <TextBlock Text="{Binding ErrorContent}" Foreground="White" Background="Red" VerticalAlignment="Center" FontWeight="Bold"/>
            </DataTemplate>
        </StackPanel.Resources>
        <TextBox Name="TextBox1" Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
        <ContentPresenter Content="{Binding ElementName= TextBox1, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>

        <TextBox Name="TextBox2" Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
        <ContentPresenter Content="{Binding ElementName= TextBox2, Path=(Validation.Errors).CurrentItem}" HorizontalAlignment="Left"/>
        <Button Content="Validate" Click="Button_Click"/>
    </StackPanel>
</Window>

The code behind file:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel _ViewModel = null;

        public MainWindow()
        {
            InitializeComponent();
            _ViewModel = new ViewModel();
            DataContext = _ViewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _ViewModel.Validate = true;
            _ViewModel.OnPropertyChanged("Text1");
            _ViewModel.OnPropertyChanged("Text2");
        }
    }
}

The ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfApplication1
{
    public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _Text1;
        public string Text1
        {
            get { return _Text1; }
            set
            {
                _Text1 = value;
                OnPropertyChanged("Text1");
            }
        }

        private string _Text2;
        public string Text2
        {
            get { return _Text2; }
            set
            {
                _Text2 = value;
                OnPropertyChanged("Text2");
            }
        }

        public bool Validate { get; set; }

        #region INotifyPropertyChanged Implemenation
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region IDataErrorInfo Implementation
        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string errorMessage = string.Empty;
                if (Validate)
                {
                    switch (columnName)
                    {
                        case "Text1":
                            if (Text1 == null)
                                errorMessage = "Text1 is mandatory.";
                            else if (Text1.Trim() == string.Empty)
                                errorMessage = "Text1 is not valid.";
                            break;
                        case "Text2":
                            if (Text2 == null)
                                errorMessage = "Text2 is mandatory.";
                            else if (Text2.Trim() == string.Empty)
                                errorMessage = "Text2 is not valid.";
                            break;
                    }
                }
                return errorMessage;
            }
        }
        #endregion
    }
}
like image 187
Anand Murali Avatar answered Sep 22 '22 18:09

Anand Murali