WPF绑定问题

时间:2009-10-25 09:35:00

标签: c# wpf binding

我有这个对象:

    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;

我收到了一堆错误,我的列表视图中没有显示任何内容:

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,而且显然具有正确的属性,为什么这不起作用呢?

3 个答案:

答案 0 :(得分:7)

看起来WPF无法直接绑定到字段,您必须使用如下属性:

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

答案 1 :(得分:3)

好的,你使用字段,但需要属性

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

}

答案 2 :(得分:-2)

查看此文章 - http://www.codeproject.com/KB/miscctrl/GridView_WPF.aspx 我认为你缺少ItemsSource =指令。