【问题标题】:WPF Overlaying a Image with a ButtonWPF用按钮覆盖图像
【发布时间】:2009-06-20 18:26:29
【问题描述】:

我有一个自定义 TabItem,上面有一个关闭按钮(用 X 表示),因此可以轻松关闭它。在此选项卡中,我想放置一个图像或边框项,以左上角的关闭按钮为中心

控制模板是

 <ControlTemplate TargetType="{x:Type local:CloseableTabItem}">
      <Grid SnapsToDevicePixels="true">
        <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" >
          <DockPanel x:Name="ContentPanel">
            <Button x:Name="PART_Close" Panel.ZIndex="1" HorizontalAlignment="Right" Margin="0,1,1,0" VerticalAlignment="Top" Width="16" Height="16" DockPanel.Dock="Right" Style="{DynamicResource CloseableTabItemButtonStyle}" ToolTip="Close Tab">
              <Path x:Name="Path" Stretch="Fill" StrokeThickness="0.5" Stroke="#FF333333" Fill="#FF969696" Data="F1 M 2.28484e-007,1.33331L 1.33333,0L 4.00001,2.66669L 6.66667,6.10352e-005L 8,1.33331L 5.33334,4L 8,6.66669L 6.66667,8L 4,5.33331L 1.33333,8L 1.086e-007,6.66669L 2.66667,4L 2.28484e-007,1.33331 Z " HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Button>
            <ContentPresenter Panel.ZIndex="0" x:Name="Content" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}"/>
          </DockPanel>
        </Border>
      </Grid>

(控制触发器已被移除) 并将边框/图像插入该选项卡标题中,我使用以下内容

<TabControl Margin="0" TabStripPlacement="Left">
        <local:CloseableTabItem>
            <local:CloseableTabItem.Header>
                <Border Height="50" Width="50" Background="Red" BorderThickness="1" BorderBrush="Black" Margin="5" />
            </local:CloseableTabItem.Header>    
        </local:CloseableTabItem>
  </TabControl>

使用这个我得到以下结果:

alt text http://lloydsparkes.co.uk/files/CloseTab.png

似乎 Button 限制了 Border 元素的宽度,所以它没有达到我设置的全宽(它设置为 50x50,所以应该是一个正方形)。我试图将它们放在不同的 Z 水平上,但它似乎没有像我预期的那样工作。

所以问题是,我怎样才能让按钮覆盖边框控件而不影响边框控件的大小?

【问题讨论】:

    标签: wpf button wpf-controls overlay border


    【解决方案1】:

    这种行为是因为这里的 DockPanel:

    <DockPanel x:Name="ContentPanel">
            <Button x:Name="PART_Close" Panel.ZIndex="1" HorizontalAlignment="Right" Margin="0,1,1,0" VerticalAlignment="Top" Width="16" Height="16" DockPanel.Dock="Right" Style="{DynamicResource CloseableTabItemButtonStyle}" ToolTip="Close Tab">
              <Path x:Name="Path" Stretch="Fill" StrokeThickness="0.5" Stroke="#FF333333" Fill="#FF969696" Data="F1 M 2.28484e-007,1.33331L 1.33333,0L 4.00001,2.66669L 6.66667,6.10352e-005L 8,1.33331L 5.33334,4L 8,6.66669L 6.66667,8L 4,5.33331L 1.33333,8L 1.086e-007,6.66669L 2.66667,4L 2.28484e-007,1.33331 Z " HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Button>
            <ContentPresenter Panel.ZIndex="0" x:Name="Content" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="{TemplateBinding Padding}"/>
          </DockPanel>
    

    删除停靠面板并根据需要调整您的属性,问题将得到解决。

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2015-06-28
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多