WPF绑定问题

pbgvytdp  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(155)

我有这个对象:

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

zour9fqk

zour9fqk1#

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

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

rqcrx0a62#

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

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

}
7uzetpgm

7uzetpgm3#

检查this article
我想你错过了ItemsSource=指令。

相关问题