向ObservableCollection添加项不会更改ListView

时间:2016-08-30 19:39:22

标签: c# binding

当我将项目添加到可观察集合时,我无法弄清楚为什么我的ListView没有更新。 这是UI的开始  enter image description here

这是我添加项目后的样子 enter image description here

无论出于何种原因,ListView没有提取工作单,也没有提取签入日期,但它确实更新了TechCount ...

这是ListView代码:

<ListView Grid.Row="2" 
        ItemsSource="{Binding LaborItems}"      
        SelectedItem="{Binding SelectedLaborItem}" 
        Margin="10,10,10,9">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="#" Width="55" DisplayMemberBinding="{Binding Labor.WorkOrderNo}"/>
            <GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding Labor.Time.CheckIn}"/>
            <GridViewColumn Header="Techs" DisplayMemberBinding="{Binding TechCount}" />
        </GridView>
    </ListView.View>
</ListView>

在图片中查看相关代码更容易,因此我做的是屏幕短代而不是原始代码 enter image description here

第26行是我点击UI上的+按钮时运行的。 TechCount与CheckIn有类似的实现,它只引用了Labor中的字段。

这是完整的viewmodel代码:

public class ServiceItemsViewModel : ViewModelBase
{
    public Labor Labor { get; set; }
    public ObservableCollection<Labor> LaborItems { get; private set; } = new ObservableCollection<Labor>();

public ServiceItemsViewModel()
{
    Labor = new Labor();
    Labor.Rates = new LaborRatesDual();
    AddLaborCommand = new DelegateCommand(addLabor, null);
}

public ICommand AddLaborCommand { get; private set; }

private void addLabor(object obj)
{
    LaborItems.Add(Labor);
    Labor = new Labor();
}

private bool canAddLabor(object arg)
{
    if (Labor.Duration.Minutes < 1)
    {
        return false;
    }

    //if (AdditionalBillingRate <= 0 || AdditionalPayoutRate <= 0)
    //{
    //    return false;
    //}

    return true;
}

public DateTime CheckIn
{
    get { return Labor.Time.CheckIn; }
    set
    {
        Labor.Time.CheckIn = value;
        RaisePropertyChanged(nameof(Labor));
    }
}
public DateTime CheckOut
{
    get { return Labor.Time.CheckOut; }
    set
    {
        Labor.Time.CheckOut = value;
        RaisePropertyChanged(nameof(Labor));
        ((DelegateCommand)AddLaborCommand).RaiseCanExecuteChanged();
    }
}
public int TechCount
{
    get { return Labor.TechCount; }
    set
    {
        Labor.TechCount = value;
        RaisePropertyChanged(nameof(Labor));
    }
}
public decimal InitialPayoutRate
{
    get { return Labor.Rates.InitialPayoutRate; }
    set
    {
        Labor.Rates.InitialPayoutRate = value;
        RaisePropertyChanged(nameof(Labor));
        //((DelegateCommand)AddLaborCommand).RaiseCanExecuteChanged();
    }
}
public decimal AdditionalPayoutRate
{
    get { return Labor.Rates.AdditionalPayoutRate; }
    set
    {
        Labor.Rates.AdditionalPayoutRate = value;
        RaisePropertyChanged(nameof(Labor));
        //((DelegateCommand)AddLaborCommand).RaiseCanExecuteChanged();
    }
}
public decimal InitialBillingRate
{
    get { return Labor.Rates.InitialBillingRate; }
    set
    {
        Labor.Rates.InitialBillingRate = value;
        RaisePropertyChanged(nameof(Labor));
        //((DelegateCommand)AddLaborCommand).RaiseCanExecuteChanged();
    }
}
public decimal AdditionalBillingRate
{
    get { return Labor.Rates.AdditionalBillingRate; }
    set
    {
        Labor.Rates.AdditionalBillingRate = value;
        RaisePropertyChanged(nameof(Labor));
        //((DelegateCommand)AddLaborCommand).RaiseCanExecuteChanged();
    }
}

private Labor selectedLaborItem;
public Labor SelectedLaborItem
{
    get { return selectedLaborItem; }
    set
    {
        selectedLaborItem = value;
        RaisePropertyChanged(nameof(Labor));
    }
}

点击+按钮后的人工内容: enter image description here

2 个答案:

答案 0 :(得分:0)

您的列绑定错误:

<GridViewColumn Header="#" Width="55" DisplayMemberBinding="{Binding **Labor.**WorkOrderNo}"/>
<GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding **Labor.**Time.CheckIn}"/>
<GridViewColumn Header="Techs" DisplayMemberBinding="{Binding TechCount}" />

请参阅上面的星号区域。

答案 1 :(得分:0)

您已经绑定了一系列人工

ItemsSource="{Binding LaborItems}" 

劳工不是劳工的财产

<GridViewColumn Header="#"    Width="55"  DisplayMemberBinding="{Binding WorkOrderNo}"/>
<GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding Time.CheckIn}"/>