【发布时间】:2010-09-01 09:27:40
【问题描述】:
我有一个典型的 MVVM 场景:我有一个 ItemsControl 绑定到 StepsViewModels 的 ObservableCollection。我定义了一个 DataTemplate,以便将 StepViewModels 呈现为 StepViews。在 StepView 中也会发生同样的情况:我有一个 ItemsControl 到一个 ObservableCollection 的 ParameterViewModels 和一个 DataTemplate 来将它们呈现为 ParameterViews。
我的问题是:我必须刷新 ItemsControl 才能呈现添加的项目和删除项目。我可以使用 StepViews 刷新ItemsControl,因为我可以访问它并且可以调用 ItemsControl.Items.Refresh()。但是如何访问 StepViews 以便调用 Refresh 方法? ItemsControl Items 是 StepViewModels...
代码如下:
<UserControl x:Class="mylib.EditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:mylib">
<UserControl.Resources>
<DataTemplate DataType="{x:Type my:StepViewModel}">
<my:StepView HorizontalAlignment="Stretch"/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<TextBlock Name="lblHelpRefresh" Margin="0 7 0 7" Visibility="{Binding HelpVisible}"
VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="9pt" Foreground="#949494"
Text="Please click refresh to reload this list"/>
<ItemsControl Name="stkStepContent"
ItemsSource="{Binding Steps}"/>
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
如何访问呈现 stkStepContent 项的 StepView 控件?
【问题讨论】:
-
如果你使用 ObservableCollections 通常不需要显式刷新;绑定检测集合何时更改并自行更新。
-
我很确定
ItemsControl需要显式刷新来更新渲染。 ItemsControl ItemsSource 绑定到 ObservableCollection 并且需要刷新。 -
正如 Alex 所说 - 如果您使用的是 ObservableCollection,则不需要刷新。
-
那么,有什么东西会导致 ItemsControl 不更新吗?
-
StepViewModel 是否包含第二个 ObservableCollection,其中包含您尝试刷新的项目?如果是这样,您可以连接一个 PropertyChanged 事件,以便当内部 ObservableCollection 更新时,它会在 StepViewModel 上触发 PropertyChanged 通知。
标签: c# .net wpf mvvm itemscontrol