2019-11-29-WPF- binding does not depend on property debugging method

Original: 2019-11-29 attribute-WPF - binding does not depend on the testing method

title author date CreateTime categories
WPF dependency property is not binding on debugging method
lindexi
2019-11-29 08:46:33 +0800
2019-8-2 18:44:5 +0800
WPF

When writing WPF program will encounter bound dependency property, but the value is not updated or is not binding on the problem, I can tell you how to debug

Dependency attribute does not correspond to

Write dependency properties, the default shortcut key is created, but if you write your own, you need to pay attention to whether the referenced class and property names corresponding string

For example, I define class Foo inside the Name property, then I should identify the registered Name and ower type is corresponding to the following code

        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string),
            typeof(Foo), new PropertyMetadata(default(string)));

If I tease modify ower type than the other types, such as the main window, then the time will fail to bind

        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string),
            typeof(MainWindow), new PropertyMetadata(default(string)));

At this point if I would write another string Name, for example, the following code is written as NameProperty string, it is not bound to rely on property by binding

        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("NameProperty", typeof(string),
            typeof(MainWindow), new PropertyMetadata(default(string)));

In the foreground XAML binding code as follows

        <TextBlock x:Name="Text" Text="{Binding Name}"></TextBlock>

Then the time will bind failure, and if the code to do the following changes to the front desk, which is binding when the name is a comment written by property name, then you can bind successfully

        <TextBlock x:Name="Text" Text="{Binding NameProperty}"></TextBlock>

Code via the code in the background or the following binding

            var binding = new Binding()
            {
                Path = new PropertyPath("NameProperty")
            };
            BindingOperations.SetBinding(Text, TextBlock.TextProperty, binding);

But this is a relatively simple way binding, will soon be seen, but there is a small partner to ask my question is can successfully bound when the background code, but the code will be written when the front desk is bound to fail, his background code as follows, when the binding is in turn is bound, the binding is right

            var binding = new Binding()
            {
                Path = new PropertyPath("Text"),
                Source = Text
            };
            BindingOperations.SetBinding(Foo, Foo.NameProperty, binding);

However, in turn written notice no name binding

        <TextBlock x:Name="Text" Text="{Binding Name}"></TextBlock>

So the discovery is not bound on the time needed first to see if the definition is not to write

Debugging using VisualStudio

You can use real-time visual tree in VisualStudio 2019 to view the properties and elements of the interface elements, in fact, this feature can be used in VisualStudio 2017

Click Debug -> Windows -> real-time visual tree you can open real-time visual tree to find the corresponding need to debug elements from the visual tree, then right-click on it to view the properties

For example, view the properties to bind the TextBlock, if you see the binding expression is, then prove the existence of at least bind

Binding property is modified

Using the binding properties when property is an expression, and if the assignment to a property, then the property will be some value

For example, I bound Name property in xaml

        <TextBlock x:Name="Text" Text="{Binding Name}"></TextBlock>

But in the implementation of certain logic will modify the property 123so again after this property will not bind and Name

            Text.Text = "123";

So how can this time to debug? You can modify the dependency properties by listening to where to get the modified value

By DependencyPropertyDescriptor.FromPropertythe method can get property methods rely on a modified, using the following codes are modified to get the text attributes

            DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty,typeof(TextBlock)).AddValueChanged(Text,
                (sender, args) =>
                {
                    Console.WriteLine("文本被修改");
                });

By Console.WriteLinethis line to add a breakpoint, the breakpoint into the discovery, through the call stack can know which business to modify the value of the property at the time of execution of the code

How to use the call stack and add breakpoints see dotnet code debugging method

Bound object not found

Another reason is the not bound may not be provided on the DataContext like, for example, I did not set a context and binding element, such as the code below

        <TextBlock x:Name="Text" Text="{Binding Name}"></TextBlock>

I hope that is bound to Name property Foo property, but in fact Text without context, can find the elements of real-time visual tree context is bound to see which class

As I see TextBlock context is actually the main window instead of the expected binding class, then you know why there is no binding on

This method is used in the list of elements inside and user controls are not binding on the list because the context in which the user controls and context may not be specified but the upper element, see WPF Frame of DataContext can not be inherited Page

Without notice

If the binding is a common CLR class, you need this class inherits INotifyPropertyChanged and then call notification method in each property needs to be notified of the above

The following is the standard wording, calling the event notification attribute modification time

        private string _name;

        public string Name
        {
            get => _name;
            set
            {
                if (value == _name) return;
                _name = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

And this time, if only to class inherits INotifyPropertyChanged interface and do not get called when the event to modify the properties, then the binding is not modified

Why be binding RelativeSource lost in the Code Behind

How to debug WPF binding

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075848.html