An integer in XAML does not result in an error

I am new to WPF / XAML. I would like to receive an error message if I bind to the wrong data type in XAML. XAML seems to want all the bindings to be through strings, but there are no error messages if you use int or double by mistake.

I found this XAML code here :

<ItemsControl ItemsSource="{Binding Path=PointList}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <!--<TextBox Text="{Binding Path=Xstr, Mode=OneWay}" />--> <Rectangle Fill="Red" Width="25" Height="25" /> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Canvas.Top" Value="{Binding Path=Ystr}" /> <Setter Property="Canvas.Left" Value="{Binding Path=Xstr}" /> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> 

I used an observable collection of PointList points (X, Y). At first I made a mistake using integers for X and Y instead of strings. This was very difficult to debug since there was no error message when trying to bind Canvas.Top to an integer. Is there a parameter in Visual Studio to catch such an error?


UPDATE

I found that the binding works on the int property, but not with the int field open. Here is the Point class I created to test this:

 class Point { public int _X; //I know this is usually private, this is to demonstrate public int _Y; //the issue with binding to a public field public string Xstr { get { return _X.ToString(); } } public string Ystr { get { return _Y.ToString(); } } public int X { get { return _X; } private set { _X = value; } } public int Y { get { return _Y; } private set { _Y = value; } } public Point(int x, int y) { _X = x; _Y = y; } } 

If I bind to an int X property or an Xstr string property, it works fine. If I try to use the public field _X, it seems that the binding cannot find the member of the class (even if it is public). Therefore, when the binding does not work, the behavior does not match the behavior in the code. An error similar to the following appears in the output window, but the application does not stop:

 System.Windows.Data Error: 40 : BindingExpression path error: '_X' property not found on 'object' ''Point' (HashCode=7877106)'. BindingExpression:Path=_X; DataItem='Point' (HashCode=7877106); target element is 'ContentPresenter' (Name=''); target property is 'Left' (type 'Double') 
0
source share
3 answers

Data binding can use any type of data. The binding system calls "ToString" or inline converters for each element to get a view for XAML. (You will get around this by writing your own converters, which is the subject of XAML.)

Others are true that .NET does not generate errors when binding problems occur. This, I told the Microsoft staff who helped write it, intentionally. They call it "unsuccessful grace."

But getting feedback with the error message is possible. To improve messaging with the binding system, find the "Tools", "Options" item in Visual Studio. Then go to the Debug section and select the Output Window property category.

One of the entries has a group called WPF Trace Settings. Change the "data binding" parameter to "Warning" and you will get debug window feedback in all of your failed bindings.

enter image description here

+2
source

it seems that XAML wants all the bindings to be through strings

This is not true. You can bind any data type that you want, as long as the property you are trying to bind and the property you are trying to bind are of the same type or have a common base type.

If this is not the case, the WPF binding mechanism offers something called Converters , which allows you to convert a restricted type into a type that the binded property has while waiting.

By default, the WPF binding mechanism calls the ToString() method for any property that you are trying to bind. in your case, calling the ToString() method on int returns the desired result, so there are no problems.

But if, for example, you used a reference type, ToString() will return the type name instead of the expected value, which will lead to unexpected behavior.

You cannot catch these types of "errors." You can:

  • View them at runtime in the visual studio output window, as indicated in the comments section.

System.Windows.Data Error: 5: The value generated by BindingExpression is not valid for the target property

  • Pay attention to what you are linking.
  • Look for type mismatches when unexpected behavior is detected in bindings.

Welcome to xaml magic :)

+3
source

First, using X and Y as a whole, rather than a string, really works.

Otherwise, if you configured the project as debug instead of realease, a large binding error will be printed to the console output.

0
source

Source: https://habr.com/ru/post/1243678/


All Articles