WPF-绑定获取属性

时间:2018-09-18 06:21:47

标签: c# wpf

我有带表格的DataGrid,我想通知另一个get属性。 这是我的示例:

ViewModel

public ObservableCollection<Data> Data
{
   get => _data;
   set {
     _data = value;
     OnPropertyChanged(nameof(Data));
   }
}

public int Summary => Data.Sum(data => data.Number);

XAML

  <DataGrid ItemsSource="{Binding Data}">
    ...
     <TextBox Title="{Binding Number, UpdateSourceTrigger=PropertyChanged"/>
    ...
  </DataGrid>

<StackPanel>
  <Label Content="Summary: "/>
  <TextBlock Text="{Binding Summary}"/>
</StackPanel>

当我将数字写入datagrid时,Summary仍为0。 当我更改集合中的值时,如何通知摘要属性? 谢谢

2 个答案:

答案 0 :(得分:2)

检查以下内容:https://www.codeproject.com/Tips/694370/How-to-Listen-to-Property-Chang

您需要的是每当集合中的任何项目发生更改时都发出通知。

更新

将显示在网格中的数据是:

public class Data
{
    public string Name { get; set; }
    private int _Salary;
    public int Salary
    {
        get
        {
            return _Salary;
        }
        set
        {
            _Salary = value;
            SalaryChanged?.Invoke();
        }
    }

    public Action SalaryChanged { get; set; }
}

代替ObservableCollection-使用此:

public class ObsCollection : ObservableCollection<Data>
{
    private Action SalaryChanged;
    public ObsCollection(Action NotifyPropertyChanged)
    {
        SalaryChanged = NotifyPropertyChanged;
    }

    protected override void InsertItem(int index, Data item)
    {
        item.SalaryChanged = SalaryChanged;
        base.InsertItem(index, item);
    }

    protected override void RemoveItem(int index)
    {            
        base.RemoveItem(index);
        SalaryChanged?.Invoke();
    }

    protected override void ClearItems()
    {
        base.ClearItems();
        SalaryChanged?.Invoke();
    }
}

最后-在视图模型中

public class MainWindowVM : INotifyPropertyChanged
{
    private ObsCollection _Data = null;

    public ObsCollection Data
    {
        get
        {
            return _Data;
        }
        set
        {
            _Data = value;
        }
    }

    public int TotalSalary
    {
        get
        {
            return _Data.Sum(d => d.Salary);
        }
    }

    public MainWindowVM()
    {
        _Data = new ObsCollection(NotifyTotalSalaryChanged);
        LoadData();
    }

    public void LoadData()
    {
        _Data.Add(new Data { Name = "Test1", Salary = 1000 });
        _Data.Add(new Data { Name = "Test2", Salary = 2000 });
        _Data.Add(new Data { Name = "Test3", Salary = 3000 });
        Notify("Data");
    }

    private void NotifyTotalSalaryChanged()
    {
        Notify("TotalSalary");
    }

    #region Property Changed Stuff
    public event PropertyChangedEventHandler PropertyChanged;

    public void Notify(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

============================================= Xaml仅仅是:

<Window x:Class="CstomObserve.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CstomObserve"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
    <local:MainWindowVM/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <DataGrid Grid.Row="0" ItemsSource="{Binding Data, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBlock Grid.Row="1" Text="{Binding TotalSalary, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>

答案 1 :(得分:1)

也许使用 try! realm.write { let json = try! JSONSerialization.jsonObject(with: jsonData!, options: []) realm.create(Post.self, value: json, update: true) //realm.add(testData.self, value: json, update: true) } 更容易。它有一个事件BindingList,如果

  • 其中一个项目引发了ListChanged事件
  • 添加了一个项目
  • 一个项目已删除
  • 物品已移动

它将为您提供检测到哪种更改(PropertyChanged)以及更改在列表中的何处发生的信息(ListChangedTypeNewIndex)。此解决方案意味着,您的OldIndex类还实现了Data

这意味着您的 viewmodel

中有以下更改
INotifyPropertyChanged