使用MVVM WPF进行数据网格绑定(Datagrid Binding Using MVVM WPF)

编程入门 行业动态 更新时间:2024-10-07 06:43:04
使用MVVM WPF进行数据网格绑定(Datagrid Binding Using MVVM WPF)

我在两个不同的地方使用mvvm面临数据网格绑定问题。 我的datagrid(xaml)的标题是:

<DataGrid Grid.Row="0" Grid.Column="0" AlternationCount="2" Background="White" RowHeight="28" HorizontalGridLinesBrush="Lavender" VerticalGridLinesBrush="Lavender" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True" ScrollViewer.CanContentScroll="True" Name="MainGrid" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="True" ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

clealy说

ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

在我的viewmodel中:

public ObservableCollection<ViewerConfiguration> Configurations { get { return m_tasks; } set { m_tasks = value; OnPropertyChanged("Configurations"); } }

列表中的数据在视图上正确显示,但问题在于插入和删除(即使它成功更新)。 我有一个在Configuration对象中插入项的函数

private void Refresh() { List<ViewerConfiguration> newlyAddedFiles = GetConfigurations(); foreach (ViewerConfiguration config in newlyAddedFiles) { Configurations.Add(config); } }

并删除像:

Configurations.Remove(configuration);

问题实际上是插入和删除。 在调试时没有异常,它也成功从集合中删除,但UI没有收到通知。 任何猜测为什么这种行为?

另外:我在datagrid下面有一个事件触发器:

<i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding RenderPdf}" CommandParameter="{Binding SelectedItem, ElementName=MainGrid}"></i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers>

我正在从ViewModel的构造函数中调用Refresh函数,只是为了查看它是否有效。

I'm facing an issue in datagrid binding using mvvm on two different places. Headers of my datagrid (xaml) is:

<DataGrid Grid.Row="0" Grid.Column="0" AlternationCount="2" Background="White" RowHeight="28" HorizontalGridLinesBrush="Lavender" VerticalGridLinesBrush="Lavender" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True" ScrollViewer.CanContentScroll="True" Name="MainGrid" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="True" ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

which clealy says

ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

In my viewmodel:

public ObservableCollection<ViewerConfiguration> Configurations { get { return m_tasks; } set { m_tasks = value; OnPropertyChanged("Configurations"); } }

The data that in the list is shown properly on the view but the problem is with the insert and delete (even it updates successfully). I have a function which inserts an item in the Configuration object

private void Refresh() { List<ViewerConfiguration> newlyAddedFiles = GetConfigurations(); foreach (ViewerConfiguration config in newlyAddedFiles) { Configurations.Add(config); } }

and removes like:

Configurations.Remove(configuration);

The problem is really with the insert and delete. On debugging there is no exception and it successfully deletes from collection too but UI doesn't get a notification. Any guesses why is this behavior?

Additionally: I have an event trigger just under the datagrid:

<i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding RenderPdf}" CommandParameter="{Binding SelectedItem, ElementName=MainGrid}"></i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers>

and I'm calling Refresh function from the constructor of ViewModel just to see if it works or not.

最满意答案

最后我解决了我遇到的问题。 基本上有两个问题。 在我的配置的getter中,我在排序和过滤前一个集合之后返回一个新的可观察集合。 第二个主要问题:

'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread'

这是我最好的猜测,我将刷新功能更改为:

private void Refresh() { try { List<ViewerConfiguration> newlyAddedFiles = GetConfigurations(); //should not update on other thread than on main thread App.Current.Dispatcher.BeginInvoke((ThreadStart)delegate() { foreach (ViewerConfiguration config in newlyAddedFiles) { Configurations.Add(config); } }); } catch (Exception ex) { } }

现在,它的工作。 谢谢

Finally I solved the issue i was having. There were basically two problems. In the getter of my Configurations, i was return a new observable collection after sorting and filtering the previous one. The second main problem:

'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread'

That was my best guess and i changed my refresh function to:

private void Refresh() { try { List<ViewerConfiguration> newlyAddedFiles = GetConfigurations(); //should not update on other thread than on main thread App.Current.Dispatcher.BeginInvoke((ThreadStart)delegate() { foreach (ViewerConfiguration config in newlyAddedFiles) { Configurations.Add(config); } }); } catch (Exception ex) { } }

Now, its working. Thanks

更多推荐

Configurations,datagrid,Refresh,删除,电脑培训,计算机培训,IT培训"/> <meta name=

本文发布于:2023-08-04 19:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   绑定   数据   WPF   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!