WPF递归用户控件

时间:2016-01-07 03:35:16

标签: c# wpf recursion user-controls

我创建了一个用户控件,旨在简化递归弹出对话框系统的使用,其中弹出对话框可以在其中打开弹出对话框。

这是用户控件的XAML:

<UserControl x:Class="SEA.WPF.View.Controls.DialogPresenter"
             Name="This"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SEA.WPF.View.Controls"
             xmlns:ts="clr-namespace:SEA.WPF.View.TemplateSelectors"
             xmlns:c="clr-namespace:SEA.WPF.View.DataConverters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <c:InstanceToVisibilityConverter x:Key="InstanceToVisibilityConverter" />
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid Visibility="{Binding Dialog, Converter={StaticResource InstanceToVisibilityConverter}}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Border Grid.Row="1" Grid.Column="1">
            <ContentControl Content="{Binding Path=Dialog, ElementName=This}"
                            ContentTemplateSelector="{Binding Path=DialogTemplateSelector, ElementName=This}" />
        </Border>
        <ContentControl Grid.RowSpan="3" Grid.ColumnSpan="3" Content="{Binding Path=Dialog.Dialog, ElementName=This}">
            <ContentControl.ContentTemplateSelector>
                <ts:InstanceOnlyTemplateSelector>
                    <ts:InstanceOnlyTemplateSelector.Template>
                        <DataTemplate>
                            <local:DialogPresenter Dialog="{Binding Path=Dialog.Dialog, ElementName=This}"
                                                   DialogTemplateSelector="{Binding Path=DialogTemplateSelector, ElementName=This}" /> 
                        </DataTemplate>
                    </ts:InstanceOnlyTemplateSelector.Template>
                </ts:InstanceOnlyTemplateSelector>
            </ContentControl.ContentTemplateSelector>
        </ContentControl>
    </Grid>
</UserControl>

以下是InstanceOnlyTemplateSelector

using System.Windows;
using System.Windows.Controls;

namespace SEA.WPF.View.TemplateSelectors
{
    public class InstanceOnlyTemplateSelector : DataTemplateSelector
    {
        public DataTemplate Template
        {
            get;
            set;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            return item == null ? null : Template;
        }
    }
}

InstaceOnlyTemplateSelector就在那里,只有在子对话框不是DialogPresenter时才会呈现递归null(从而避免了递归绑定导致的堆栈溢出)。

问题是,当子对话框从null更改为其他内容时,递归DialogPresenter不会像它的父级一样呈现。我尝试将DialogPresenter替换为GridBackground等于Brushes.Black,这会使整个屏幕变黑(如预期的那样)。

为什么DialogPresenter的递归实例没有按预期呈现?

0 个答案:

没有答案