如何解决MVVM WPF - 如何更新绑定到 ObservableCollection 的 DataGrid
我想做什么:
我有一个 WPF 应用程序,链接到一个 SQL 服务器。我正在使用 MVVM-light 包(我确实安装了 Prism.Core,但我不确定我是否在使用它.... MVVM 新手)。
有一个 DataGrid
,绑定到一个 ObservableCollection
。
我一直在尝试实施 PropertyChangedEventHandler
,但似乎无法让它发挥作用。
我绑定了一个删除按钮,我可以删除行,但是当我重新打开表单时,更改不会继续。
我尝试将 DataGrid
的绑定模式从 OneWay
更改为 TwoWay
。使用 OneWay
,当我重新打开表单时,更改不会继续。使用 TwoWay
,我在打开子表单(包含 DataGrid
)时收到此错误消息:
System.InvalidOperationException: 'A TwoWay 或 OneWayToSource 绑定不能在'Ridel.Hub.ViewModel.LicenseHoldersViewModel'类型的只读属性'licenseHolders'上工作。'
所以,如果我在 set;
中添加一个 public ObservableCollection<LicenseHolders> licenseHolders { get; }
,
程序运行,但之前的问题仍然存在,就像 OneWay
上有 DataGrid
模式配置时一样。
如果不直接与 Sql-server
通信,我需要做些什么才能使其正常工作,这首先违背了使用此方法的全部意义?
视图模型:
public class LicenseHoldersViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<LicenseHolders> licenseHolders { get; }
= new ObservableCollection<LicenseHolders>();
public LicenseHoldersViewModel() {
FillDataGridLicenseHolders();
}
private void FillDataGridLicenseHolders() {
try {
using (SqlConnection sqlCon = new(ConnectionString.connectionString))
using (SqlCommand sqlCmd = new("select * from tblLicenseHolder",sqlCon))
using (SqlDataAdapter sqlDaAd = new(sqlCmd))
using (DataSet ds = new()) {
sqlCon.Open();
sqlDaAd.Fill(ds,"tblLicenseHolder");
foreach (DataRow dr in ds.Tables[0].Rows) {
licenseHolders.Add(new LicenseHolders {
ID = Convert.ToInt32(dr[0].ToString()),Foretaksnavn = dr[1].ToString(),Foretaksnummer = dr[2].ToString(),Adresse = dr[3].ToString(),Postnummer = (int)dr[4],Poststed = dr[5].ToString(),BIC = dr[6].ToString(),IBAN = dr[7].ToString(),//Profilbilde ???
Kontaktperson = dr[8].ToString(),Epost = dr[9].ToString(),Tlf = dr[10].ToString()
});
}
}
} catch (Exception ex) {
MessageBox.Show(ex.Message,"Message",MessageBoxButton.OK,MessageBoxImage.Information);
}
}
private RelayCommand<LicenseHolders> _removeLicenseHoldersCommand;
public RelayCommand<LicenseHolders> RemoveLicenseHoldersCommand => _removeLicenseHoldersCommand
??= new RelayCommand<LicenseHolders>(RemoveLicenseHolderExecute,RemoveLicenseHolderCanExecute);
private bool RemoveLicenseHolderCanExecute(LicenseHolders myLicenseHolder) {
// Checking for the removeable licenseholder in the collection
return licenseHolders.Contains(myLicenseHolder);
}
private void RemoveLicenseHolderExecute(LicenseHolders myLicenseHolder) {
licenseHolders.Remove(myLicenseHolder);
}
private void OnPropertyChanged(string myLicenseHolder) {
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(myLicenseHolder));
}
}
型号
public class LicenseHolders {
public int ID { get; set; }
public string Foretaksnavn { get; set; }
public string Foretaksnummer { get; set; }
public string Adresse { get; set; }
public int Postnummer { get; set; }
public string Poststed { get; set; }
public string BIC { get; set; }
public string IBAN { get; set; }
public string Kontaktperson { get; set; }
public string Epost { get; set; }
public string Tlf { get; set; }
}
代码隐藏
public partial class Personell : Window {
LicenseHoldersViewModel licenseHoldersViewModel;
public Personell() {
InitializeComponent();
btnLogOut.Content = UserInfo.UserName;
licenseHoldersViewModel = new LicenseHoldersViewModel();
base.DataContext = licenseHoldersViewModel;
}
XAML
<Window x:Class="Ridel.Hub.Personell"
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:Ridel.Hub" xmlns:viewmodel="clr-namespace:Ridel.Hub.ViewModel" d:DataContext="{d:DesignInstance Type=viewmodel:LicenseHoldersViewModel}"
mc:Ignorable="d"
<Canvas Margin="10,10,10">
<!-- I click this button when I want to delete a DataGrid row -->
<Button Style="{DynamicResource ButtonWithRoundCornersGreen}" FontSize="22" x:Name="btnDelete" Content="Delete license holder" Width="187" Height="47"
Background="#48bb88" Foreground="White" Canvas.Left="547" Canvas.Top="668" IsEnabled="False"
Command="{Binding RemoveLicenseHoldersCommand}" CommandParameter="{Binding SelectedItem,ElementName=dgLicenseHolder}"/>
<DataGrid
x:Name="dgLicenseHolder"
CanUserAddRows="False"
ItemsSource="{Binding licenseHolders,Mode=TwoWay}"
Height="557"
Width="505"
ColumnWidth="*"
Canvas.Top="158"
FontSize="20"
IsReadOnly="True"
SelectionMode="Single"
AutoGenerateColumns="False"
CanUserDeleteRows="False"
SelectionChanged="dgLicenseHolder_SelectionChanged" Canvas.Left="31" >
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path='ID'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Foretaksnavn" Binding="{Binding Path='Foretaksnavn'}" IsReadOnly="True" Visibility="Visible"/>
<DataGridTextColumn Header="Foretaksnummer" Binding="{Binding Path='Foretaksnummer'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Adresse" Binding="{Binding Path='Adresse'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Postnummer" Binding="{Binding Path='Postnummer'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Poststed" Binding="{Binding Path='Poststed'}" IsReadOnly="True" Visibility="Visible"/>
<DataGridTextColumn Header="BIC" Binding="{Binding Path='BIC'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="IBAN" Binding="{Binding Path='IBAN'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Kontaktperson" Binding="{Binding Path='Kontaktperson'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Epost" Binding="{Binding Path='Epost'}" IsReadOnly="True" Visibility="Collapsed"/>
<DataGridTextColumn Header="Tlf" Binding="{Binding Path='Tlf'}" IsReadOnly="True" Visibility="Collapsed"/>
</DataGrid.Columns>
</DataGrid>
</Canvas>
</Window>
解决方法
你在混淆话题。 VM 需要您拥有但未使用的 InotifyPropertyChanged
事件来通知前端中的 Xaml VM 属性已更改并绑定到新数据参考。
这对于 List
或 ObservableCollection
是必需的。完成此操作后,ObservableCollection
将在添加或删除项目时发送有关列表更改的通知。
因为你错过了第一步:
public ObservableCollection<LicenseHolders> licenseHolders { get; }
= new ObservableCollection<LicenseHolders>();
前端没有得到引用的改变。因为它没有 set { _myCollection = value; OnPropertyChanged( "licenseHolders "); }
Xaml 不知道将绑定从最初的 null
更改为对列表的有效引用,并且您会得到一个无效的视觉控件。
坦率地说,如果您不分页信息(在初始运行后添加或删除 ObservableCollection
),ObservableCollection
可以替换为一个简单的 List...只需将行添加到有效的局部变量列表,并在最后 then 将其设置为 licenseHolders
列表,前端 xaml 将起作用。
我认为行的删除会反映在服务器上,因为 ObservableCollection 是如何绑定的。
这不是真的。
ObservableCollection 仅提供其更改的通知。
也就是说,如果您向 ObservableCollection 添加了一个项目,那么它会自动出现在 DataGrid 中。
如果您在没有通知的情况下使用集合(例如 List),则不会发生这种情况。
并且您将不得不想出一些额外的技术来触发视图的更新。
ObservableCollection“不知道”它的元素是如何创建的,它们是如何与数据源(数据库、文件或其他源)相关的。
在您的 previous topic 中,在我的示例代码中,我展示了 bool RemoveFromBD(LicenseHolders license)
方法只是为了编写将 license
从数据库中删除到其中的代码。
我不太了解 ADO,我根本不知道您数据库中表字段的名称,但也许该方法的这种实现是正确的:
private bool RemoveFromBD(LicenseHolders license)
{
string sql = string.Format("Delete from tblLicenseHolder where ID = '{0}'",license.ID);
using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectionString))
using (SqlCommand cmd = new SqlCommand(sql,sqlCon))
{
try
{
sqlCon.Open();
cmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。