WPF绑定问题 [英] WPF Binding Problem

查看:165
本文介绍了WPF绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的对象:

    class a 
    { 
        public string Application; 
        public DateTime From, To;
    }

和我宣布这个名单吧:

    ObservableCollection<a> ApplicationsCollection = 
        new ObservableCollection<a>();

在我的XAML我有:

    <ListView Height="226.381" Name="lstStatus" Width="248.383" HorizontalAlignment="Left" Margin="12,0,0,12" VerticalAlignment=">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Application"
                                DisplayMemberBinding="{Binding Path=Application}"/>
                <GridViewColumn Width="50" Header="From" 
                                DisplayMemberBinding="{Binding Path=From}"/>
                <GridViewColumn Width="50" Header="To" 
                                DisplayMemberBinding="{Binding Path=To}"/>
            </GridView>
        </ListView.View>
    </ListView>

当我做的:

        lstStatus.ItemsSource = ApplicationsCollection;

我收到了一堆错误,并没有在我的列表视图中显示出来:

I get a bunch of errors and nothing shows up in my list view:

System.Windows.Data Error: 39 : BindingExpression path error: 'Application' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=Application; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'From' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=From; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'To' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=To; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

这显然看到该对象为具有类型 A 和显然有正确的性质,那么,为什么不是这个工作?

It's obviously seeing the object as having type a and a's obviously have the correct properties, so why isn't this working?

推荐答案

看起来像WPF无法绑定到字段直接,你必须使用属性,像这样:

Looks like WPF can't bind to fields directly, you have to use properties like so:

class a
{
    public string Application { get; set; }
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}

这篇关于WPF绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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