Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从绑定项控件访问我的视图_C#_.net_Wpf_Mvvm_Itemscontrol - Fatal编程技术网

C# 从绑定项控件访问我的视图

C# 从绑定项控件访问我的视图,c#,.net,wpf,mvvm,itemscontrol,C#,.net,Wpf,Mvvm,Itemscontrol,我有一个典型的MVVM场景:我有一个ItemsControl绑定到StepsViewModels的ObservableCollection。我定义了一个DataTemplate,以便将StepViewModels呈现为StepViews。在StepView中也会发生同样的情况:我有一个ItemsControl到参数视图模型的observeCollection,使用数据模板将它们呈现为参数视图 我的问题是:我必须刷新ItemsControl以呈现添加的项目和删除的项目。我可以使用StepView刷

我有一个典型的MVVM场景:我有一个
ItemsControl
绑定到StepsViewModels的
ObservableCollection
。我定义了一个DataTemplate,以便将StepViewModels呈现为StepViews。在StepView中也会发生同样的情况:我有一个
ItemsControl
到参数视图模型的
observeCollection
,使用数据模板将它们呈现为参数视图

我的问题是:我必须刷新
ItemsControl
以呈现添加的项目和删除的项目。我可以使用StepView刷新
ItemsControl
,因为我可以访问它,并且可以调用ItemsControl.Items.refresh()。但是如何访问步骤视图以便调用刷新方法呢?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控件?

要访问代码隐藏中的视图,可以使用控件的DataContext并将其强制转换到相应的视图中。例如:

ObservableCollection<StepViewModel> vmCollection = 
    stkStepContent.DataContext as ObservableCollection<StepViewModel>;

foreach(StepViewModel vm in vmCollection)
{
    vm.Refresh();
}
ObservableCollection vmCollection=
stkStepContent.DataContext作为ObservableCollection;
foreach(vmCollection中的StepViewModel虚拟机)
{
vm.Refresh();
}

编辑:StepViewModel是否包含第二个ObservableCollection,其中包含您试图刷新的项?如果是这样,您可能能够连接PropertyChanged事件,这样当内部ObservableCollection更新时,它会在StepViewModel上触发PropertyChanged通知。如果您有ObservableCollection,则项目的添加和删除应自动刷新。但是,如果您刚刚更新了StepsViewModel中的属性,那么StepsViewModel应该是通过实现INotifyPropertyChanged接口或从DependencyObject继承并改用DependencyProperties属性来提醒更新的对象


无论如何,如果确实希望能够手动刷新集合,请在ViewModel中使用CollectionViewSource。然后调用CollectionViewSource的View.Refresh()方法。

如果使用ObservableCollections,通常不需要显式刷新;绑定检测集合何时更改并更新自身。我非常确定
ItemsControl
需要显式刷新才能更新呈现。ItemsControl ItemsSource绑定到ObservaleCollection,需要刷新。正如Alex所说,如果使用ObservaleCollection,则不需要刷新。那么,是否有什么原因会导致ItemsControl不更新?StepViewModel是否包含第二个ObservaleCollection,其中包含您尝试刷新的项?如果是这样,您可能能够连接PropertyChanged事件,这样当内部ObservableCollection得到更新时,它会在StepViewModel上触发PropertyChanged通知。我将此答案标记为已接受,但正是您的评论真正解决了我的问题。添加到ObservableCollection时,我没有调用PropertyChanged。相当愚蠢的错误。编辑了我的答案,包含了你提到的评论。在写这个答案的时候,我实际上是想添加那个问题,但我想我会把它作为一个评论,因为它是一个问题,而不是一个答案:)