简体   繁体   English

我的WPF列表视图不会绑定

[英]My WPF listview will not bind

This may seem an odd question but I am stuck. 这似乎是一个奇怪的问题,但我被困住了。

I am using ListViews in my wpf app that dynamically binds to list collections. 我在动态绑定到列表集合的wpf应用程序中使用ListViews。

Apart from one. 除了一个。 I just cannot see what is wrong/different. 我只是看不出出什么问题/不同。

This is my model: 这是我的模型:

public class WorkUnitCost
{
    public string ErrorMessage { get; set; }
    public int WorkUnitCostId { get; set; }
    public bool IsUploaded { get; set; }
    public string WorkUnitCostRef { get; set; }
    public string Description { get; set; }
    public double Cost { get; set; }
    public bool IncludesVAT { get; set; }
    public string CompanyRef { get; set; }
    public bool Active { get; set; }
    public bool Exists { get; set; }
    public string Caption
    {
        get { return Description + " £" + Cost; }
    }
}

This is my collection: 这是我的收藏:

public List<WorkUnitCost> WorkUnitCost = new List<Model.WorkUnitCost>();

This is my code-behind: 这是我的代码背后:

lvWorkItems.ItemsSource = WorkUnitCost;

This is my markup: 这是我的标记:

     <ListView x:Name="lvWorkItems">           
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Description" Width="190" DisplayMemberBinding="{Binding Description}"  />
                <GridViewColumn Header="IsVat" Width="50" DisplayMemberBinding="{Binding IncludesVAT}" />
                <GridViewColumn Header="Cost" Width="75" DisplayMemberBinding="{Binding Cost}" />
            </GridView>
        </ListView.View>
    </ListView>

By setting a break-point I know there are 2 items with populated fields. 通过设置断点,我知道有2个项目具有填充字段。 Yet, nothing is visually displayed? 但是,什么都看不到吗?

If you are adding items to your colleciton after setting it as ItemsSource , your UI won't get notified about changes. 如果您在将项目设置为ItemsSource之后向其添加项目,则不会收到有关更改的通知。

You will have to use some collection implementing INotifyCollectionChanged instead. 您将不得不使用一些实现INotifyCollectionChanged集合。 ObservableCollection<T> is most common choice. ObservableCollection<T>是最常见的选择。

I think you need to implement The INotifyCollectionChanged to your model and instead of List try to use ObservableCollection<WorkUnitCost> to enable your view synchronize with your model data. 我认为您需要对模型实现INotifyCollectionChanged ,而不是List尝试使用ObservableCollection<WorkUnitCost>来使视图与模型数据同步。 Actually It will be better if you use MVVM to ensure the best practices for WPF , you will find at this link a good explanation of how to use ObservableCollection and INotifyPropertyChanged with MVVM design pattern 实际上,如果您使用MVVM来确保WPF的最佳做法会更好,那么您将在此链接上找到有关如何将ObservableCollectionINotifyPropertyChangedMVVM设计模式一起使用的很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM