Is it possible to write new attached property like Validation.Errors, say MyValidation.XYZ

Alexander Landa 0 Reputation points
2024-03-25T15:29:46.77+00:00

Hello,

I would like to use some set of my own different attached properties not defined in Validation.cs.

Actually I've tried all known possible ways including AddValueChanged for existing public Validation.<DependencyProperty>, but no have no luck - I get only a default value of new property and has no chance to get an actualization event fired.

Thank you for assistance.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,924 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,026 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2024-03-26T07:32:43.25+00:00

    Hi,@Alexander Landa. Welcome to Microsoft Q&A. It is possible to create your own attached properties in WPF, similar to Validation.Errors, for various purposes such as custom validation, behavior addition, or data binding facilitation.

    Here's an example of create a custom attached property MyValidation.XYZ:

    <StackPanel>
    
        <TextBox Width="100" Height="60" Text="{Binding MyValue}" local:MyValidation.XYZ="{Binding MyValue}" />
    
    </StackPanel>
    

    Codebehind:

    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
        private string myValue;
    
        public string MyValue
        {
            get { return myValue; }
            set { myValue = value; OnPropertyChanged(); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    
    }
    public static class MyValidation
    {
        public static readonly DependencyProperty XYZProperty =
            DependencyProperty.RegisterAttached("XYZ", typeof(string), typeof(MyValidation),
                new PropertyMetadata(null, OnXYZPropertyChanged));
    
        public static string GetXYZ(DependencyObject obj)
        {
            return (string)obj.GetValue(XYZProperty);
        }
    
        public static void SetXYZ(DependencyObject obj, string value)
        {
            obj.SetValue(XYZProperty, value);
        }
    
        private static void OnXYZPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // This method will be called whenever the value of XYZ property changes
            // You can perform any necessary actions here
            string newValue = (string)e.NewValue;
            string oldValue = (string)e.OldValue;
    
            // Example: Logging the change
            System.Diagnostics.Debug.WriteLine($"XYZ property changed from '{oldValue}' to '{newValue}'");
        }
    }
    
    
    

    The result:

    When MyValue changes, a meesage is displayed in the output window.

    XYZ property changed from '' to 'FFF' XYZ property changed from 'FFF' to 'DDD'


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.