簡體   English   中英

如何在xaml中創建可重復使用的窗口內容?

[英]How can I create re-usable window content in xaml?

有可能做這樣的事情嗎?

    <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>

您好,本文演示了實現此目的的幾種方法。

如何在WPF控件中嵌入任意內容

如@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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM