2014-07-01 31 views
1

是否有可能做这样的事情?如何在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> 
+0

你能尝试和澄清这一点?我不确定你想要完成什么。 – BradleyDotNET

+0

我做了你所要求的编辑。让我知道我是否可以添加更多细节。我对缺乏清晰度表示歉意。谢谢你指出。我正在创建一个应用程序,并且我的确遇到了问题,我遇到了很多麻烦。 –

+0

澄清更好,你可能会删除“我的情况”部分,因为它只是混淆(至少对我来说)。我已投票重新开放,并会发布一个答案,如果它重新打开,但看看'ContentControl'在同时 – BradleyDotNET

回答

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> 

相关问题