WPF:如何创建将常见外部XAML排除在外的'wrapper'视图?

问题描述 投票:0回答:1

在WPF项目中,我正在创建一个包含许多步骤的向导。每个步骤都有自己的看法。当前,每个视图都具有相同的外部XAML元素,例如该步骤的标题为TextBlock,但其下的内容不同。

因此,例如,典型视图如下:

STEP X VIEW

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Step X text goes here"/>
    <Grid Grid.Row="1">
        <!-- CONTENT OF STEP X GOES HERE -->
    </Grid>     
</Grid>

我想做的是能够将每个步骤的公共外部XAML“分解”到另一个视图中,只需将每个步骤的内容作为该新分解出的视图的内容。这样,我可以避免重复相同的外部XAML,并且如果我决定更改每个步骤标题的实现,则只需要在一个地方进行即可。

我确定ContentPresenter是解决方案的一部分。因此,类似:

WRAPPERVIEW:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="Step X text goes here"/>
    <Grid Grid.Row="1">
        <ContentPresenter Content="Content of Step X ??????"/>
    </Grid>
</Grid>

我的问题是:

1)我的WrapperView实现是正确的方法吗?

2)ContentPresenterWrapperView内容属性的绑定是什么?

3)如何使用Text为每一步自定义TextBlockWrapperView

4)如何使用WrapperView获取任何步骤视图的XAML的客户端XAML。

因此,将WrapperView用于StepXView,

New STEP X VIEW

<Grid>
    <WrapperView TitleText ="Step X Title Text">
        <!-- XAML for Step X View -->
    </WrapperView>
</Grid>

感谢您的帮助。

c# wpf xaml
1个回答
1
投票

您对使用ContentPresenter有很好的直觉。对我来说,看起来就像您需要一个HeaderedContentControl,这是一个可处理ControlHeader属性的可模板化Content

尽管您可以直接使用HeaderedContentControl,但如果以后要对其进行进一步的自定义,我发现可以更清晰地继承它:

public class WrapperView : HeaderedContentControl { }

并且在您的XAML中,您可以对其进行模板化并根据需要使用它:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1">
    <Window.Resources>
        <Style TargetType="{x:Type local:WrapperView}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:WrapperView">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <ContentPresenter HorizontalAlignment="Center" Grid.Row="0" Content="{TemplateBinding Header}"/>
                            <Grid Grid.Row="1" HorizontalAlignment="Center">
                                <ContentPresenter Content="{TemplateBinding Content}"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <local:WrapperView Header ="Step X Title Text">
            <Rectangle Width="50" Height="50" Fill="Blue"/>
        </local:WrapperView>
    </Grid>
</Window>

这里的关键是使用TemlateBinding来回答您的第二个问题。

为了使其更加整洁,您可以将Style移离Window.Resources并将其设置为Themes\Generics.xaml,以便默认使用Style

© www.soinside.com 2019 - 2024. All rights reserved.