gpt4 book ai didi

c# - WPF绑定(bind)问题

转载 作者:行者123 更新时间:2023-11-30 14:04:35 25 4
gpt4 key购买 nike

我有这个对象:

    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;

我遇到了一堆错误,但我的 ListView 中没有显示任何内容:

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

最佳答案

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

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

关于c# - WPF绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1620466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com