在ListView中更新对象的属性后如何获取列表的对象

时间:2019-01-24 08:54:16

标签: c# wpf xaml mvvm

我正在使ListView充满对象列表,这些属性在ListView中显示并可以编辑。当其属性正在更新时,我需要获取对象。我该怎么办?

我尝试创建类的对象并将其绑定到ListView中的SelectedItem。问题是,很显然,在单击ListItem的行后设置了SelectedItem,但没有单击该行的子级。每次更改任何ComboBox或TextBox值后,我都需要从ListView的行中获取更新的对象。

要使用INotifyPropertyChanged处理所有事情,我正在使用PropertyChanged.Fody。它可以帮助我更轻松地解决此问题吗?

查看

Appearance of the ListView

<ListView 
        Margin="10"
        Grid.Row="1"
        Grid.ColumnSpan="2"
        ItemsSource="{Binding TimesheetEntries}"
        SelectedItem="{Binding SelectedEntry, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        >
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="30" Margin="3">
                    <TextBlock 
                        Text="{Binding Date, StringFormat=dd-MM-yyyy}" 
                        VerticalAlignment="Center" 
                        Width="Auto" 
                        Margin="10"/>
                    <ComboBox 
                        SelectedValuePath="Key" DisplayMemberPath="Value" 
                        ItemsSource="{Binding EmploymentTypesDictionary, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                        SelectedValue="{Binding SelectedEmployment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        Width="270"/>
                    <TextBox 
                        Text="{Binding Hours, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                        Margin="10,0,0,0"
                        Width="70"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

ViewModel

public List<TimesheetEntryEntity> TimesheetEntries
{
    get { return _TimesheetEntries; }
    set { _TimesheetEntries = value; }
}

public TimesheetEntryEntity SelectedEntry
{
    get { return _SelectedEntry; }
    set { _SelectedEntry = value; }
}

...

private List<TimesheetEntryEntity> _TimesheetEntries { get; set; }
private TimesheetEntryEntity _SelectedEntry;
private TimesheetModel timesheetModel;
public TimesheetViewModel()
{
        this.Timesheets = TimesheetUnitModel.GetAllTimesheetsForUnit((int)Application.Current.Properties["UnitID"]);
        this._StartDate = DateTime.Now;
        _TimesheetEntries = new List<TimesheetEntryEntity>();
}
public KeyValuePair<int, string> SelectedWorker
{
    get { return _SelectedWorker; }
    set
    {
        _SelectedWorker = value;
        _TimesheetEntries =
                timesheetModel.GetTimesheetList(_SelectedWorker.Key, SelectedTimesheet.Key, StartDate.Date);
    }
}

TimesheetEntryEntity

    public DateTime Date { get; set; }

    public Dictionary<EmploymentTypes, string> EmploymentTypesDictionary { get; set; }

    public EmploymentTypes SelectedEmployment {
        get { return _SelectedEmployment; }
        set
        {
            _SelectedEmployment = value;
            CheckHoursAvaliability();
        }
    }
    public bool HoursAvaliable { get; set; }

    public decimal Hours
    {
        get;
        set;
    }
    private EmploymentTypes _SelectedEmployment;

    public TimesheetEntryEntity()
    {
        FillEmploymentTypes();
    }

    public void FillEmploymentTypes()
    {
        //Some code here
    }

我尝试遵循Get Object properties of selected list item问题的答案,但是只有文本块,因此无论如何都选择了该行,但是我有ComboBox和TextBox,他们有自己的重点。

1 个答案:

答案 0 :(得分:0)

您可以在TimesheetEntryEntity中实现INotifyPropertyChanged,即

public abstract class TimesheetEntryEntity: INotifyPropertyChanged
{
    public event EventHandler Changed;
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public void OnChange()
    {
        EventHandler handler = Changed;
        handler?.Invoke(this, EventArgs.Empty);
    }
    private DateTime date;
    public DateTime Date
    {
        get => date;
        set
        {
            if (date == value)
            {
                return;
            }
            //Do something with unchanged property
            date = value;
            RaisePropertyChanged();
            OnChange();
            //Do something with changed property
        }
    }
在您的ViewModel中

在将新项目添加到列表之前:

   timesheet.Changed+=ItemChanged;

 private void ItemChanged(object sender, EventArgs e)
    {
       var item=sender as TimesheetEntryEntity;
       //do something    
    }