使用 MVVM WPF 的数据网格绑定
Datagrid Binding Using MVVM WPF
我在两个不同的地方使用 mvvm 绑定数据网格时遇到问题。我的数据网格 (xaml) 的 Headers 是:
<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}">
清楚地说
ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
在我的视图模型中:
public ObservableCollection<ViewerConfiguration> Configurations
{
get { return m_tasks; }
set { m_tasks = value; OnPropertyChanged("Configurations"); }
}
列表中的数据在视图中正确显示,但问题在于插入和删除(即使更新成功)。
我有一个在配置 object
中插入项目的功能
private void Refresh()
{
List<ViewerConfiguration> newlyAddedFiles = GetConfigurations();
foreach (ViewerConfiguration config in newlyAddedFiles)
{
Configurations.Add(config);
}
}
并删除像:
Configurations.Remove(configuration);
问题确实出在插入和删除上。在调试时没有异常,它也成功地从 collection 中删除,但 UI 没有收到通知。猜猜为什么会出现这种行为?
另外:
我在数据网格下面有一个事件触发器:
<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 函数只是为了看看它是否有效。
我终于解决了我遇到的问题。基本上有两个问题。在我的配置的 getter 中,我是 return 在对前一个进行排序和过滤之后的一个新的可观察集合。
第二个主要问题:
'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)
{
}
}
现在,它的工作。
谢谢
我在两个不同的地方使用 mvvm 绑定数据网格时遇到问题。我的数据网格 (xaml) 的 Headers 是:
<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}">
清楚地说
ItemsSource="{Binding Path=Configurations, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
在我的视图模型中:
public ObservableCollection<ViewerConfiguration> Configurations
{
get { return m_tasks; }
set { m_tasks = value; OnPropertyChanged("Configurations"); }
}
列表中的数据在视图中正确显示,但问题在于插入和删除(即使更新成功)。 我有一个在配置 object
中插入项目的功能private void Refresh()
{
List<ViewerConfiguration> newlyAddedFiles = GetConfigurations();
foreach (ViewerConfiguration config in newlyAddedFiles)
{
Configurations.Add(config);
}
}
并删除像:
Configurations.Remove(configuration);
问题确实出在插入和删除上。在调试时没有异常,它也成功地从 collection 中删除,但 UI 没有收到通知。猜猜为什么会出现这种行为?
另外: 我在数据网格下面有一个事件触发器:
<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 函数只是为了看看它是否有效。
我终于解决了我遇到的问题。基本上有两个问题。在我的配置的 getter 中,我是 return 在对前一个进行排序和过滤之后的一个新的可观察集合。 第二个主要问题:
'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)
{
}
}
现在,它的工作。 谢谢