WPF启动器按钮位于工具栏选项卡旁边,但不在内部

如何解决WPF启动器按钮位于工具栏选项卡旁边,但不在内部

我不知道如何让按钮Razveljavi停留在OsnovnoVstavi旁边。我不希望它出现在工具栏中,而是在右边,因此无论您在哪个选项卡上,它始终显示。

有一幅我所拥有的以及我想要的样子的照片。如果有人可以帮助我,那就太好了。我肯定这只是一个小问题,但我只是从WPF开始。

Screenshot of the application

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="200"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="22"/>
        <RowDefinition Height="55"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="25"/>
    </Grid.RowDefinitions>

    <Menu Grid.Row="0" Grid.ColumnSpan="2">
        <MenuItem Header="Datoteka">
            <MenuItem Header="Odpri"/>
            <MenuItem Header="Shrani"/>
            <MenuItem Header="Izhod" Click="izhod"/>
        </MenuItem>
        <MenuItem Header="Uredi">
            
        </MenuItem>
        <MenuItem Header="Orodja">
            
        </MenuItem>
    </Menu>
    <TabControl Grid.Row="1" Grid.ColumnSpan="2">
        <TabItem Header="Osnovno">
            <ToolBarTray>
                <ToolBar>
                    <Button Content="Pisava"/>
                    <Button Content="Barva" Click="spreminjanjeBarve"/>
                </ToolBar>
                <ToolBar>
                    <Button Content="Kopiraj" Click="kopiraj">
                    </Button>
                    <Button Content="Izreži"/>
                    <Button Content="Prilepi" Click="prilepi"/>
                </ToolBar>
                <ToolBar>
                    <Button Content="Razveljavi"/>
                    <Button Content="Ponovi"/>
                </ToolBar>
            </ToolBarTray>
            
        </TabItem>
        <TabItem Header="Vstavi"/>
    </TabControl>
    <RichTextBox Name="box" Grid.Row="2" Background="LightBlue" GotFocus="RichTextBox_GotFocus" LostFocus="RichTextBox_LostFocus">
        <FlowDocument>
            <Paragraph Name="tekst">
                Vpiši tekst
                <Bold>tukaj</Bold> .
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
    <StatusBar Grid.Row="3">
        <TextBlock Name="status"/>
    </StatusBar>
</Grid>

解决方法

这并不像看起来那么容易。您本质上想要做的就是将控件和功能添加到现有控件中。为了将按钮添加到选项卡条中,您必须创建一个自定义控件模板,因为它定义了控件的外观和视觉状态。要向这些按钮添加功能,您要么必须创建自定义的附加属性,要么创建一个自定义控件。

我个人认为,更简洁的方法是为此方案创建自定义控件。为了使其灵活,我们在标签栏中创建了一个区域,用于存储任何其他内容,甚至允许为其定义数据模板。

首先,创建一个派生自TabControl的类,并为要显示的内容添加依赖性属性,并为任意内容添加可选的数据模板。

public class MyTabControl : TabControl
{
   static MyTabControl()
   {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTabControl),new
         FrameworkPropertyMetadata(typeof(MyTabControl)));
   }

   public static readonly DependencyProperty AdditionalTabStripContentTemplateProperty = DependencyProperty.Register(
      nameof(AdditionalTabStripContentTemplate),typeof(DataTemplate),typeof(MyTabControl),new PropertyMetadata(null));

   public static readonly DependencyProperty AdditionalTabStripContentProperty = DependencyProperty.Register(
      nameof(AdditionalTabStripContent),typeof(object),new PropertyMetadata(null));

   public DataTemplate AdditionalTabStripContentTemplate
   {
      get => (DataTemplate)GetValue(AdditionalTabStripContentTemplateProperty);
      set => SetValue(AdditionalTabStripContentTemplateProperty,value);
   }

   public object AdditionalTabStripContent
   {
      get => GetValue(AdditionalTabStripContentProperty);
      set => SetValue(AdditionalTabStripContentProperty,value);
   }
}

通过复制并改编TabControl的默认样式(可以为extracted using Blend or Visual Studio),为带有附加标签栏内容的新标签控件创建样式。

<SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
<SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
<Style x:Key="MyTabControlStyle" TargetType="{x:Type local:MyTabControl}">
   <Setter Property="Padding" Value="2"/>
   <Setter Property="HorizontalContentAlignment" Value="Center"/>
   <Setter Property="VerticalContentAlignment" Value="Center"/>
   <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
   <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyTabControl}">
            <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition x:Name="ColumnDefinition0"/>
                  <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
               </Grid.ColumnDefinitions>
               <Grid.RowDefinitions>
                  <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                  <RowDefinition x:Name="RowDefinition1" Height="*"/>
               </Grid.RowDefinitions>
               <StackPanel Orientation="Horizontal">
                  <TabPanel x:Name="headerPanel" Background="Transparent" IsItemsHost="true" Margin="2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                  <ContentControl Content="{TemplateBinding AdditionalTabStripContent}" ContentTemplate="{TemplateBinding AdditionalTabStripContentTemplate}" Height="{Binding Height,ElementName=headerPanel}" Margin="0,0"/>
               </StackPanel>
               <Border x:Name="contentPanel" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="2">
                  <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
               </Border>
            </Grid>
            <ControlTemplate.Triggers>
               <Trigger Property="TabStripPlacement" Value="Bottom">
                  <Setter Property="Grid.Row" TargetName="headerPanel" Value="1"/>
                  <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                  <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                  <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                  <Setter Property="Margin" TargetName="headerPanel" Value="2,2"/>
               </Trigger>
               <Trigger Property="TabStripPlacement" Value="Left">
                  <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                  <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                  <Setter Property="Grid.Column" TargetName="headerPanel" Value="0"/>
                  <Setter Property="Grid.Column" TargetName="contentPanel" Value="1"/>
                  <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                  <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                  <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                  <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                  <Setter Property="Margin" TargetName="headerPanel" Value="2,2"/>
               </Trigger>
               <Trigger Property="TabStripPlacement" Value="Right">
                  <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                  <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                  <Setter Property="Grid.Column" TargetName="headerPanel" Value="1"/>
                  <Setter Property="Grid.Column" TargetName="contentPanel" Value="0"/>
                  <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                  <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                  <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                  <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                  <Setter Property="Margin" TargetName="headerPanel" Value="0,2"/>
               </Trigger>
               <Trigger Property="IsEnabled" Value="false">
                  <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

此样式与默认样式几乎相同。区别在于以下几行。我们使用StackPanel在标签标题面板的右侧添加一个ContentControl,并将其内容和模板绑定到自定义控件的属性。我们还将内容控件的Height绑定到标签页眉面板,因此看起来很干净。

<StackPanel Orientation="Horizontal">
   <TabPanel x:Name="headerPanel" Background="Transparent" IsItemsHost="true" Margin="2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
   <ContentControl Content="{TemplateBinding AdditionalTabStripContent}" ContentTemplate="{TemplateBinding AdditionalTabStripContentTemplate}" Height="{Binding Height,0"/>
</StackPanel>

要结束样式,请创建隐式样式,以便将其自动应用于范围中的所有MyTabControl

<Style TargetType="{x:Type local:MyTabControl}" BasedOn="{StaticResource MyTabControlStyle}"/>

现在用此替换XAML中的TabControl,并添加标签栏内容,例如:

<local:MyTabControl Grid.Row="1" Grid.ColumnSpan="2">
   <local:MyTabControl.AdditionalTabStripContent>
      <StackPanel Orientation="Horizontal">
         <Button Content="Razveljavi"/>
         <Button Content="Ponovi"/>
      </StackPanel>
   </local:MyTabControl.AdditionalTabStripContent>
   <!-- ...your other tab items. -->
</local:MyTabControl>

这很酷的事情是您可以在此处和even data template it添加任何内容。结果:

Result with StackPanel

此模板的唯一缺点是选项卡页眉无法折叠成多行。如果您将上方的StackPanel替换为DockPanel,则会得到响应速度更快的版本,但是它将使右边框上的按钮永久对齐。

<DockPanel LastChildFill="True">
   <ContentControl DockPanel.Dock="Right" Content="{TemplateBinding AdditionalTabStripContent}" ContentTemplate="{TemplateBinding AdditionalTabStripContentTemplate}" Height="{Binding Height,0"/>
   <TabPanel DockPanel.Dock="Left" x:Name="headerPanel" Background="Transparent" IsItemsHost="true" Margin="2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
</DockPanel>

正常大小的窗口和迫使标签页眉折叠的小窗口的结果:

Result with DockPanel

Result with DockPanel small

,

我会创建一个带有标题的空假TabItem,其中除了Button之外什么都不包含:

<TabControl>
    <TabItem Header="1 2 3 4 5 6">
        <Border Background="Aqua"/>
    </TabItem>

    <TabItem Header="A B C D E F">
        <Border Background="Bisque"/>
    </TabItem>

    <TabItem>
        <TabItem.Template>
            <ControlTemplate TargetType="TabItem">
                <ContentPresenter Content="{TemplateBinding Header}"/>
            </ControlTemplate>
        </TabItem.Template>

        <TabItem.Header>
            <StackPanel Orientation="Horizontal" Margin="5,0">
                <Button Content="C L I C K" Margin="2" Padding="10,3" />
                <Button Content="H E L L O" Margin="2" Padding="10,3"/>
            </StackPanel>
        </TabItem.Header>

        <Border Background="Chartreuse"/>
    </TabItem>
</TabControl>

按钮会拦截鼠标单击,并且不会触发选项卡选择。

Header buttons

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res