简体   繁体   English

数据库更新后更新 Listview (WPF + MVVM)

[英]Update Listview after database has updated (WPF + MVVM)

I've got a small WPF form with a Listview (bound to an observablecolletion)我有一个带有 Listview 的小型 WPF 表单(绑定到 observablecolletion)

    <ListView Name="Employees_Listview" ItemsSource="{Binding Employees, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding YourSelectedItem, Mode=TwoWay}" Height="86" IsSynchronizedWithCurrentItem="True">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"/>
                <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"/>
            </GridView>
        </ListView.View>
    </ListView>

2 textboxes 2个文本框

<TextBox Name="FirstName" Width="136" Text="{Binding SelectedItem.FirstName, ElementName=Employees_Listview, Mode=TwoWay}" />
<TextBox Name="LastName" Width="136" Text="{Binding SelectedItem.LastName, ElementName=Employees_Listview, Mode=TwoWay}" />

and a button和一个按钮

<Button Command="{Binding UpdateEmployeeCommand}" Margin="0,0,10,0">Update</Button>

Both textboxes are bound to the selected item in listview so when I update a textbox value it will automatically reflect the changes in the listview.两个文本框都绑定到列表视图中的选定项目,因此当我更新文本框值时,它将自动反映列表视图中的更改。 Now I would like to update the database first and if there is no error I would like to update the listview.现在我想先更新数据库,如果没有错误我想更新列表视图。 My update code looks like this我的更新代码如下所示

    var container = ContainerConfig.Configure();
    using (var scope = container.BeginLifetimeScope())
    {
        var SelectedEmployee = scope.Resolve<IEmployeeRepository>();
        if (!SelectedEmployee.Update(YourSelectedItem))
        {
            MessageBox.Show("Datensatz konnte nicht aktualisiert werden!" + "\n" + "Bitte den Administrator verständigen!");
            return;
        };
    }

I tried to set the textbox mode to OneWay but due the fact it's bound to the SelectedItem I can't get the new textbox value, only the "old" listview value.我试图将文本框模式设置为 OneWay 但由于它绑定到 SelectedItem 我无法获得新的文本框值,只有“旧”列表视图值。 How can I check and update my database first before refreshing the listview?如何在刷新列表视图之前先检查和更新我的数据库?

UPDATE Maybe I have to choose another approach like this: TextBox bound a new property declared更新也许我必须选择这样的另一种方法: TextBox bound a new property declaration

View看法

<TextBox Text="{Binding NewFirstName}" Width="123"></TextBox>

and my ViewModel和我的 ViewModel

//TEST
private string newfirstname;


public string NewFirstName
{
    get { return newfirstname; }
    set
    {
        newfirstname= value;
        RaisePropertyChanged("NewFirstName");
    }
}
//TEST
public EmployeeEntity YourSelectedItem
{
    get
    {
        return _yourSelectedItem;
    }
    set
    {
        NewFirstName = value.FirstName;
        _yourSelectedItem = value;
        RaisePropertyChanged("YourSelectedItem");
    }
}

Guess this is not the best way to go:/猜猜这不是 go 的最佳方式:/

I would do it like this:我会这样做:

  1. Update the database.更新数据库。
  2. Either return the updated data entity from the update method, or, query the database after the update to retrieve the updated data entity.从更新方法返回更新的数据实体,或者在更新后查询数据库以检索更新的数据实体。
  3. Update the object in your application with the new values.使用新值更新应用程序中的 object。

I would strongly recommend having a unique identifier/ID both in your database and in your classes within the application to facilitate this if you don't already.我强烈建议在您的数据库和应用程序的类中都有一个唯一的标识符/ID,如果您还没有这样做的话。

Also, you'd need to have INotifyPropertyChanged implemented on your Model classes, as updating an item within an ObservableCollection won't have any impact on your View, as ObservableCollection only raises notifications when the collection itself changes, eg, add/remove, and not when properties within its members change.此外,您需要在您的 Model 类上实现INotifyPropertyChanged ,因为更新ObservableCollection中的项目不会对您的视图产生任何影响,因为ObservableCollection仅在集合本身发生更改时引发通知,例如添加/删除和不是当其成员中的属性发生变化时。

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

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