2014-07-01 28 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> 

相關問題