具有DataContext绑定的多层MVVM模式

时间:2019-02-24 14:22:47

标签: c# wpf

我进入一个应用程序,并以MVVM模式使用此WPF UI框架。

当我将模式与MainWindow一起使用并且一个ContentControl正常工作时,但是当我想将相同的逻辑应用于子ContentControl时,会收到此错误

System.InvalidOperationException: 'Layout recursion reached allowed limit to avoid stack overflow: '2047'. Either the tree contains a loop or is too deep.'

这就是我对用户界面的看法

单击“图像”或“保存”按钮时,主窗口将打开这些部分:

 <Window.Resources>
    <DataTemplate x:Name="imageViewTemplate" DataType="{x:Type viewmodels:ImageFiltersViewModel}">
        <views:ImageFiltersView DataContext="{Binding}"></views:ImageFiltersView>
    </DataTemplate>
</Window.Resources>

<Grid Background="#2A304D">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Grid.Column="0">
        <StackPanel>
            <Image Source="Content/menu.png" Width="25" Margin="15" MouseLeftButtonDown="MenuViewShow"/>
        </StackPanel>
    </DockPanel>

    <DockPanel Grid.Row="1" Grid.Column="0">
        <Image Source="Content/folder.png" Width="25" Margin="15" Visibility="Visible" Name="FolderButton" />
    </DockPanel>
    <DockPanel Grid.Row="2" Grid.Column="0">
        <Image Source="Content/image.png" Width="25" Margin="15" Visibility="Visible" Name="ImageButton" MouseLeftButtonDown="ShowImageSettings">
        </Image>
    </DockPanel>
    <DockPanel Grid.Row="3" Grid.Column="0">
        <Image Source="Content/save.png" Width="25" Margin="15" Visibility="Visible" Name="SaveButton"/>
    </DockPanel>

    <ContentControl Grid.RowSpan="7" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="7" Content="{Binding}"></ContentControl>
    <!--<ContentControl Grid.ColumnSpan="6" Grid.Column="1" Grid.Row="0" Content="{Binding}"></ContentControl>-->


</Grid>

“图像过滤器”视图以相同的逻辑其他视图打开:

<UserControl.Resources>
    <DataTemplate x:Name="imageViewFilterTemplate" DataType="{x:Type viewmodels:ImageFilterOptionOne}">
        <views:ImageFilterOptionOne DataContext="{Binding ElementName=ImageContentView}"></views:ImageFilterOptionOne>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Grid.Column="0">
        <StackPanel>
            <Image Source="../Content/menu.png" Width="25" Margin="15" MouseLeftButtonDown="ShowFilterOneOptions"/>
        </StackPanel>
    </DockPanel>

    <DockPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="6" Grid.RowSpan="5" Background="#565555"></DockPanel>
    <ContentControl Grid.Row="6" Grid.ColumnSpan="8" Grid.Column="0" Content="{Binding}" x:Name="ImageContentView"></ContentControl>
</Grid>

我认为问题出在DataContext绑定上,可能我需要将DataTemplate定位为绑定对象或类似对象,但我没有发现任何具体内容。

0 个答案:

没有答案