XAML 中的绑定整数不会导致错误 [英] Binding integer in XAML does not result in error

查看:25
本文介绍了XAML 中的绑定整数不会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WPF/XAML 的新手.如果我绑定到 XAML 中的错误数据类型,我想收到一条错误消息.XAML 似乎希望所有绑定都通过字符串进行,但是如果您错误地使用了 int 或 double,则不会出现错误消息.

I am a newbie to WPF/XAML. I would like to get an error message if I bind to the wrong data type in XAML. It seems that XAML wants all binding to be through strings, but there are no error messages if you use an int or double by mistake.

我找到了这个 XAML 代码 此处:

I have 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>

我使用了一个可观察的集合 PointList of Points(X,Y).一开始我犯了一个错误,即使用整数代替 X 和 Y 而不是字符串.这很难调试,因为在尝试将 Canvas.Top 绑定到整数时没有错误消息.Visual Studio 中是否有设置可以捕获此类错误?

I have used an observable collection PointList of Points(X,Y). I made the mistake at first of 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 the Canvas.Top to an integer. Is there a setting in Visual Studio to be able to catch this kind of error?

我发现绑定适用于 int 属性,但不适用于公共 int 字段.这是我创建的一个 Point 类来测试这个:

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

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;
    }
} 

如果我绑定到 int 属性 X 或字符串属性 Xstr,它就可以正常工作.如果我尝试使用公共字段 _X 那么绑定似乎找不到类成员(即使它是公共的).因此,当绑定失败时,行为与代码中的异常不同.输出窗口中显示如下错误,但应用程序并未停止:

If I bind to the int property X or string property Xstr it works fine. If I try to use the public field _X then it seems the binding cannot find the class member (even though it is public). So when a binding fails, the behavior is not the same as an exception in your code. An error like the following shows up 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')

推荐答案

数据绑定可以使用任何数据类型.绑定系统在每个项目上调用ToString"或内置转换器以获取 XAML 的表示形式.(您可以通过编写自己的转换器来解决这个问题,转换器本身就是一个 XAML 主题.)

A data binding can use any data type. The binding system calls "ToString" or built-in converters on each item to get a representation for XAML. (You get around that by writing your own converters, which is a XAML subject all its own.)

其他人是正确的,当绑定出现问题时 .NET 不会抛出错误.这是,帮助编写它的 Microsoft 员工告诉我,这是故意的.他们称之为优雅地失败".

The others are correct that .NET doesn't throw errors when there are problems with binding. This is, I've been told by the Microsoft employees who helped write it, intentional. They call it "failing gracefully".

但是获得错误消息反馈是可能的.要从绑定系统获得更好的错误消息,请在 Visual Studio 中找到工具"、选项"菜单项.然后,转到调试"部分并选择输出窗口"属性类别.

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

其中一个条目有一个名为WPF 跟踪设置"的组.将数据绑定"选项更改为警告",您将获得有关所有失败绑定的调试窗口反馈.

One of the entries there is a group called "WPF Trace Settings". Change the "Data Binding" option to "Warning" and you'll get debug-window feedback on all your failed bindings.

这篇关于XAML 中的绑定整数不会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆