如何在xaml中创建可重用的窗口内容?

时间:2014-07-01 19:41:20

标签: c# wpf xaml window

是否可以做这样的事情?

    <Window> 
    <MyCustomXamlTemplateForWindows>
        <Content>
            <MySpecifiedUserControlForThisParticularWindow/>
        </Content>
    </MyCustomXamlTemplateForWindows>
    </Window>

其中<Content>进入自定义xaml模板的指定元素。

这是我的实际代码,我希望“内容网格”更通用:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:PrimaryCommand}">
        <Button Content="{Binding Content}" Command="{Binding Command}" Height="20" Width="74" Margin="5,0,0,0"/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SecondaryCommand}">
        <TextBlock Height="20" Margin="5,0,0,0">
                    <Hyperlink Command="{Binding Command}">
                        <Run Text="{Binding Content}"></Run>
                    </Hyperlink>
        </TextBlock>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SeparatorCommand}">
        <TextBlock Height="20" Margin="5,0,0,0" Text="|"/>
    </DataTemplate>
</Window.Resources>

    <Grid ShowGridLines="False">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="12*"/>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="77*"/>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="5*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="92*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="6*"/>
    </Grid.RowDefinitions>
    <Button Content="Xx." Command="{Binding HideAllViews}" Height="20" Width="32" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="4" Grid.Row="3"/>

    <ItemsControl ItemsSource="{Binding NavModel.NavCommands}" Grid.Column="0" Grid.Row="1">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Height="20" Margin="10,10,0,0">
                    <Hyperlink Command="{Binding Command}">
                        <Run Text="{Binding Content}"></Run>
                    </Hyperlink>
                </TextBlock>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <ItemsControl ItemsSource="{Binding CommandModel.Commands}" Grid.Column="2" Grid.Row="3"  HorizontalAlignment="Right">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

    <ItemsControl ItemsSource="{Binding HelpModel.HelpCommands}" Grid.Column="4"  Grid.Row="1">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Height="32" Width="32" Margin="0,0,0,7" Content="{Binding Content}" Command="{Binding Command}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <Grid x:Name="ContentGrid" Grid.Column="2" Grid.Row="1" >
        <TextBlock.Text>
             Content goes here.
        </TextBlock.Text>

    </Grid>
</Grid>

1 个答案:

答案 0 :(得分:1)

您好这篇文章演示了实现此目的的几种方法。

How to Embed Arbitrary Content in a WPF Control

如@BradleyDotNET所述,您可以使用ContentControl

 <<Application ...>
<Application.Resources>
    <ControlTemplate x:Key="Decorator" TargetType="ContentControl">
        <StackPanel Orientation="Vertical" >
            <Label>Foo</Label>
            <ContentPresenter />
            <Label>Bar</Label>
        </StackPanel>
    </ControlTemplate>
</Application.Resources>

<Window ... >
<StackPanel Orientation="Vertical">
    <ContentControl Template="{StaticResource Decorator}">
        <Label Background="Yellow">User supplied content here</Label>
    </ContentControl>
</StackPanel>