Click here to Skip to main content
15,844,602 members
Home / Discussions / WPF
   

WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 4:09
cofounderChris Maunder16-Jul-09 4:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 4:05
cofounderChris Maunder16-Jul-09 4:05 
QuestionGet Tab For Context Menu Pin
Kevin Marois26-Jan-24 19:35
professionalKevin Marois26-Jan-24 19:35 
QuestionINotifyDataErrorInfo Question Pin
Kevin Marois2-Jan-24 18:07
professionalKevin Marois2-Jan-24 18:07 
QuestionWhen Are All DependencyProperties Set Pin
Kevin Marois28-Dec-23 22:12
professionalKevin Marois28-Dec-23 22:12 
QuestionDataTemplate Problem Pin
Kevin Marois5-Dec-23 13:08
professionalKevin Marois5-Dec-23 13:08 
AnswerRe: DataTemplate Problem Pin
Richard Deeming5-Dec-23 23:03
mveRichard Deeming5-Dec-23 23:03 
AnswerDataGrid Event Not Firing Pin
Kevin Marois30-Nov-23 14:11
professionalKevin Marois30-Nov-23 14:11 
GeneralRe: DataGrid Event Not Firing Pin
Andre Oosthuizen1-Dec-23 23:42
mveAndre Oosthuizen1-Dec-23 23:42 
GeneralRe: DataGrid Event Not Firing Pin
Kevin Marois2-Dec-23 8:41
professionalKevin Marois2-Dec-23 8:41 
QuestionDataGrid Exception Pin
Kevin Marois28-Nov-23 12:36
professionalKevin Marois28-Nov-23 12:36 
AnswerRe: DataGrid Exception Pin
Richard Deeming28-Nov-23 23:30
mveRichard Deeming28-Nov-23 23:30 
GeneralRe: DataGrid Exception Pin
Kevin Marois29-Nov-23 8:12
professionalKevin Marois29-Nov-23 8:12 
GeneralRe: DataGrid Exception Pin
Kevin Marois29-Nov-23 9:13
professionalKevin Marois29-Nov-23 9:13 
GeneralRe: DataGrid Exception Pin
Richard Deeming29-Nov-23 23:22
mveRichard Deeming29-Nov-23 23:22 
GeneralRe: DataGrid Exception Pin
Kevin Marois30-Nov-23 7:46
professionalKevin Marois30-Nov-23 7:46 
QuestionCustom Control Style Question Revisited Pin
Kevin Marois30-Oct-23 10:14
professionalKevin Marois30-Oct-23 10:14 
AnswerRe: Custom Control Style Question Revisited Pin
Gerry Schmitz30-Oct-23 13:20
mveGerry Schmitz30-Oct-23 13:20 
GeneralRe: Custom Control Style Question Revisited Pin
Kevin Marois31-Oct-23 9:01
professionalKevin Marois31-Oct-23 9:01 
QuestionUI Validation Problem Pin
Kevin Marois10-Oct-23 14:10
professionalKevin Marois10-Oct-23 14:10 
I am implementing INotifyDataErrorInfo. Here's my base ViewModel
public class _ViewModelBase : BindableBase, INotifyDataErrorInfo
{
    #region Events
    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    #endregion

    #region Private Fields
    public readonly Dictionary<string, List<string>> _validationErrors = new Dictionary<string, List<string>>();
    #endregion

    #region Properties
    public bool HasErrors => _validationErrors.Any();
    #endregion

    #region Public Methods
    public IEnumerable GetErrors(string propertyName)
    {
        return _validationErrors.ContainsKey(propertyName) ? _validationErrors[propertyName] : null;
    }
    #endregion

    #region Protected Methods
    protected void AddError(string propertyName, string error)
    {
        if (!_validationErrors.ContainsKey(propertyName))
        {
            _validationErrors[propertyName] = new List<string>();
        }

        if (!_validationErrors[propertyName].Contains(error))
        {
            _validationErrors[propertyName].Add(error);
            RaiseErrorsChanged(propertyName);
        }
    }

    protected void ClearErrors(string propertyName)
    {
        if (_validationErrors.ContainsKey(propertyName))
        {
            _validationErrors.Remove(propertyName);
            {
                RaiseErrorsChanged(propertyName);
            }
        }
    }
    #endregion

    #region Private Methods
    private void RaiseErrorsChanged(string propertyName)
    {
        ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
    }
    #endregion
}
Here's my CustomerView
<TextBlock Grid.Row="0"
           Text="Customer Name"/>
<TextBox Grid.Row="1"
         Grid.Column="0"
         Text="{Binding CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
         IsEnabled="{Binding AreFieldsEnabled}">

    <Validation.ErrorTemplate>
        <ControlTemplate>
            <StackPanel>
                <AdornedElementPlaceholder x:Name="textBox" />
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ErrorContent}" Foreground="Red" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>

</TextBox>
Here's my CustomerViewModel (parts ommited)
private void Validate()
{
    ClearErrors(nameof(Customer.CustomerName));

    if (string.IsNullOrWhiteSpace(Customer.CustomerName))
    {
        AddError(nameof(Customer.CustomerName), "The Customer Name cannot be empty.");
    }

}

private void Customer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    IsChanged = true;

    Validate();
    RaiseChangedEvent();
}

The problem is that if I use complex nation, as in Customer.CustomerName instead of just CustomerName, the validation text does not appear. The error gets added to the collection, but I don't see it.

What's wrong here?
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.

AnswerRe: UI Validation Problem Pin
Richard Deeming10-Oct-23 23:13
mveRichard Deeming10-Oct-23 23:13 
GeneralRe: UI Validation Problem Pin
Kevin Marois12-Oct-23 8:25
professionalKevin Marois12-Oct-23 8:25 
GeneralRe: UI Validation Problem Pin
Richard Deeming12-Oct-23 22:34
mveRichard Deeming12-Oct-23 22:34 
AnswerRe: UI Validation Problem Pin
Gerry Schmitz12-Oct-23 9:52
mveGerry Schmitz12-Oct-23 9:52 
GeneralRe: UI Validation Problem Pin
Kevin Marois12-Oct-23 15:54
professionalKevin Marois12-Oct-23 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.