dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.05k stars 1.17k forks source link

Two ways binding NullReferenceException #4969

Open Cricle opened 3 years ago

Cricle commented 3 years ago

Configuration

Name Version
dotnet tool 5.0.302
target framework net452, net461, net472

Problem

When I use background binding, provided Path, Source and set mode TwoWay, always throw NullReferenceException , but in net5.0 no exception, but how can i do when in NET Framework?

Whether i use SetBinding or BindingOperations.SetBinding.

StackTrack

   at MS.Internal.AvTrace.AntiFormat(String s)
1

Code

var q = new Binding();
q.Path = new PropertyPath("RotateTransform");
q.Source = obj.RotateTransformDesigner;
q.Mode = BindingMode.TwoWay;
Bd.SetBinding(Control.RenderTransformProperty, q);
BindingOperations.SetBinding(Bd, Control.RenderTransformProperty, q);//ERROR
lindexi commented 3 years ago

@Cricle Is the Bd null?

是不是 Bd 这个值是空的?

Cricle commented 3 years ago

@Cricle Is the Bd null?

是不是 Bd 这个值是空的?

No, it is define in xaml. 不是,它是在XAML定义的

1
SamBent commented 3 years ago

@Cricle Please attach a full repro, and more of the stack trace. Also a crash dump, if possible.

AvTrace.AntiFormat in the stack trace suggests that someone is trying to print an error message to the trace output sink (typically the VS output window). Does this happen in .NET 5.0?

A null-reference error suggests that the error message is null. This shouldn't happen, but perhaps something is wrong with your installation of .NET Framework, maybe the error message lookup fails because of a localization problem. A crash dump might shed light on this; the XAML and code-behind is definitely not enough.

Cricle commented 3 years ago

@Cricle Please attach a full repro, and more of the stack trace. Also a crash dump, if possible.

AvTrace.AntiFormat in the stack trace suggests that someone is trying to print an error message to the trace output sink (typically the VS output window). Does this happen in .NET 5.0?

A null-reference error suggests that the error message is null. This shouldn't happen, but perhaps something is wrong with your installation of .NET Framework, maybe the error message lookup fails because of a localization problem. A crash dump might shed light on this; the XAML and code-behind is definitely not enough.

I'm sorry that the original project can't be provided, but I'm trying to write a sample. Please wait me some days. Thanks.

The StackTrace is here

1

VS show error line

designUnits.Add(unit);//ERROR

designUnits type is ObservableCollection<T> , unit is a normal clr object, but it contains a Bound instance and a UIElement properties.

VS Version VS 2019 Communtity 16.10.4

singhashish-wpf commented 2 years ago

@Cricle Is it possible to provide the minimal repro?

Cricle commented 2 years ago

@Cricle Is it possible to provide the minimal repro?

I'll try

miloush commented 2 years ago

OK so '{0}' (Name='{1}') is only in TraceData.DescribeSourceObject, which means that targetElement is not null in DescribeTarget and we know it's a FrameworkElement otherwise DescribeSourceObject would not try to print the Name string.

Now the next call is:

https://github.com/dotnet/wpf/blob/89d172db0b7a192de720c6cfba5e28a1e7d46123/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/TraceData.cs#L197

where

https://github.com/dotnet/wpf/blob/89d172db0b7a192de720c6cfba5e28a1e7d46123/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/AvTrace.cs#L569-L572

so either arg1 or arg2 is null here.

arg1 is a type name which presumably is not null, especially since it's a type deriving from FrameworkElement.

Ergo the FrameworkElement's Name must be null.

miloush commented 2 years ago

The purpose of AntiFormat is to escape a string to be passed to the Format method, so it should not be used on arguments, only the message. Plus, I think checking for and transparently passing null is a good hygiene for any escaping/unescaping routine.

Cricle commented 2 years ago

image I found!!!

image It will given null value!

The code

public class ASetting
    {
        public string Ax { get; set; }
    }
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var t = new TextBox();
            var data = new ASetting();
            //When I bind control name alway null
            t.SetBinding(NameProperty, new Binding("Ax") { Source = data });
            //I set an error binding, it must throw NullReferenceException
            t.SetBinding(TextBox.AcceptsReturnProperty, new Binding
            {
                Source = data,
                Path=new PropertyPath("aaa")
            });
        }
    }

@singhashish-wpf @miloush @SamBent @lindexi

lindexi commented 2 years ago

@Cricle Yes, I think it is the WPF bug. Thank you.

            var t = new TextBox()
            {
                Name = null, // Set the Name to null
            };
            var data = new ASetting();
            ////When I bind control name alway null
            ////t.SetBinding(NameProperty, new Binding("Ax") { Source = data });
            //I set an error binding, it must throw NullReferenceException
            t.SetBinding(TextBox.AcceptsReturnProperty, new Binding
            {
                Source = data,
                Path = new PropertyPath("aaa")
            });

All the code : https://github.com/lindexi/lindexi_gd/tree/b89cee95091997cda801cf20f6042d30f7f85518/KedenejekiLallcallyine