2

所以我一直在做一个项目,该项目允许我将选项卡添加到选项卡控件以及每个新的TabItem. 我有一堆以编程方式生成的控件。有没有办法在 XAML 中创建一个模板,我可以简单地添加到新创建的模板中TabItem

举个简单的例子,假设我有一个带有 TabPage 的 WPF 表单:

<Window>
 <Grid>
  <TabControl></Tabcontrol>
 </Grid> 
</Window>

我想在我的每个 TabPages 中添加 3 个按钮。

<Window>
 <Grid>
  <TabControl>
   <TabItem>
    <Button/>
    <Button/>
    <Button/>
   </TabItem>
  </TabControl>
 </Grid> 
</Window>

我希望能够在 XAML 编辑器(可能是 VS2012)中编辑这个包含 3 个按钮的模板,并且这些更改将反映在添加到每个TabItem.

我环顾四周,大多数 WPF 模板文章都是关于着色或样式模板的,这似乎不是我想要的。根据我目前的方法,以TabItem编程方式添加是可行的,但会使编辑模板更加麻烦,而且我认为这与 WPF 的理念相反:将代码与设计断开连接。

我对 WPF 相当陌生,但已经看到它相对于 WinForms 的强大功能和优势,所以我在这条路上被卖掉了,但因为我是新手,我的术语有时有点混乱,这也使我的搜索尝试失败,时。

任何帮助,将不胜感激。如果我解释得不够好,请告诉我。

4

4 回答 4

1

您应该尝试使用UserControl

它将允许您使用 - 例如 - 一组三个按钮作为简单控件。它是由一个 xaml 文件定义的,对这个 xaml 文件的更改将反映在添加用户控件的任何位置。

于 2013-01-07T10:40:16.677 回答
1

正如@Olwaro 提到的,您可能想要使用UserControl

给定您的示例,您可以UserControl使用此 Xaml(其中用户控件称为 ThreeButtonStack 并驻留在命名空间 WpfApplication2 中):

<UserControl x:Class="WpfApplication2.ThreeButtonStack"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="5" />
            <Setter Property="Width" Value="75" />
        </Style>
    </UserControl.Resources>
    <StackPanel Orientation="Horizontal">
        <Button Content="OK" />
        <Button Content="Cancel" />
        <Button Content="Retry" />
    </StackPanel>
</UserControl>

然后你可以像这样在你的主应用程序中调用它:

<Window x:Class="WpfApplication2.TabPageWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        Title="TabPageWindow" Height="300" Width="300">
    <TabControl>
        <TabItem Header="Tab 1">
            <DockPanel>
                <wpfApplication2:ThreeButtonStack DockPanel.Dock="Bottom" HorizontalAlignment="Right"/>
                <TextBlock>Custom Content</TextBlock>
            </DockPanel>
        </TabItem>
        <TabItem Header="Tab 2">
            <DockPanel>
                <wpfApplication2:ThreeButtonStack DockPanel.Dock="Bottom" HorizontalAlignment="Right"/>
                <TextBox>Editing custom stuff</TextBox>
            </DockPanel>
        </TabItem>
    </TabControl>
</Window>

结果将是:

截屏

于 2013-01-07T11:28:03.883 回答
1

TabItem最终继承自ContentControl,这意味着您可以以一种可以装饰内容控件中显示的实际内容的方式对其进行模板化:

<Style TargetType="TabItem">
  <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TabItem}">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ContentPresenter Grid.Row="0" />
            <StackPanel Grid.Row=1>
              <!-- Your buttons -->
            </StackPanel>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
  </Setter>
</Style>

放置ContentPresenter的位置是放置 ContentControl(或在本例中为 TabItem)的内容的位置。

例如,我们使用它来为集合显示添加搜索/突出显示功能。您可能想要复制默认模板并从那里修改 - 这是丑陋的部分,因为在模板中练习标记重用是相当困难的。

或者,您将 TabItem 保持原样,并以您在 TabItems 中使用的所需方式设置 ContentControl 的样式...

<TabItem>
  <ContentControl Style="{StaticResource CommonButtons}">
    <!-- TabItem Contents -->
  </ContentControl>
</TabItem>

当然,ContentControl 位可以在 DataTemplate 中结束,并且通过绑定到ItemsSource你最终得到

<TabControl 
   ItemTemplate="{StaticResource CommonButtonsTemplate}" 
   ItemsSource="{Binding MyTabItemsData}" />
于 2013-01-07T11:34:07.900 回答
0

我不知道类 TabPage ,但要解决此类问题,最好创建一个继承 TabPage 并在其中工作的类,并在其中设置通用功能,而不是使用模板。我希望这会有所帮助。

于 2013-01-04T14:06:08.653 回答