.net 如何为 WPF 元素提供矩形平面 3D 边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/581838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:11:37  来源:igfitidea点击:

How can I give a WPF element a rectangular flat 3D border?

.netwpfborder

提问by Drew Noakes

I would like to create a rectangular 'flat 3D' look for one of my control templates. In it's most simple version this means having a line at the bottom that is darker than that at the top, and maybe some variation between the left and right lines too.

我想为我的一个控件模板创建一个矩形的“平面 3D”外观。在最简单的版本中,这意味着底部的线条比顶部的线条更暗,并且左右线条之间可能也有一些变化。

A more complex version would allow me to provide on or more brushes so that gradients could be applied.

更复杂的版本将允许我提供一个或多个画笔,以便可以应用渐变。

The default <Border>element in WPF lets you specify a different thickness per edge, but I can't find a way to specify multiple brushes.

<Border>WPF 中的默认元素允许您为每条边指定不同的厚度,但我找不到指定多个画笔的方法。

So, how can I produce the effect I want as simply as possible?

那么,我怎样才能尽可能简单地产生我想要的效果呢?

EDITit's been suggested that I post an example of how I want to use this. Personally I'd be happy to have a style or a user control. The user control might be used thus:

编辑有人建议我发布一个我想如何使用它的例子。就我个人而言,我很乐意拥有一种样式或用户控件。用户控件可以这样使用:

<FourSidedBorder LeftSideBrush="#00f" RightSideBrush="#0f0" ... />

Or perhaps even simpler:

或者甚至更简单:

<FourSidedBorder BorderBrush="#00f,#0f0,#f00,#fff"
                 BorderThickness="1,2,3,4" ... />

These are just ideas. Any sensible, concise solution is welcome.

这些只是想法。欢迎任何明智、简洁的解决方案。

回答by Drew Noakes

Here is a solution I devised that achieves most of what I want. It doesn't give complete control over all four sides independently, but it does give the rectangular flat 3D view that I want.

这是我设计的一个解决方案,可以实现我想要的大部分内容。它不能独立地完全控制所有四个边,但它确实提供了我想要的矩形平面 3D 视图。

Here's how it looks:

这是它的外观:

Paste this into Kaxamlto see it for yourself:

将其粘贴到Kaxaml 中以亲自查看:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Background="#CCC">
  <Page.Resources>
    <!-- A brush for flat 3D panel borders -->
    <LinearGradientBrush x:Key="Flat3DBorderBrush"
                         StartPoint="0.499,0" EndPoint="0.501,1">
      <GradientStop Color="#FFF" Offset="0" />
      <GradientStop Color="#DDD" Offset="0.01" />
      <GradientStop Color="#AAA" Offset="0.99" />
      <GradientStop Color="#888" Offset="1" />
    </LinearGradientBrush>
  </Page.Resources>
  <Grid>  
    <!-- A flat 3D panel -->
    <Border
          HorizontalAlignment="Center" VerticalAlignment="Center"
          BorderBrush="{StaticResource Flat3DBorderBrush}"
          BorderThickness="1" Background="#BBB">

          <!-- some content here -->
          <Control Width="100" Height="100"/>

    </Border>  
  </Grid>
</Page>

Hope that helps someone else out. I'm still on the lookout for innovative solutions to this problem, so keep posting and I'll accept a better answer than this one.

希望能帮助别人。我仍在寻找解决此问题的创新解决方案,因此请继续发帖,我会接受比这个更好的答案。

回答by Greg D

I've done something like this just by placing multiple borders directly on top of one another. E.g.:

我只是通过将多个边框直接放在另一个边框上来完成类似的操作。例如:

<Border 
  x:Name="TopAndLeft" 
  BorderThickness="3,3,0,0" 
  BorderBrush="{x:Static SystemColors.ControlLightBrush}">
<Border 
  x:Name="BottomAndRight" 
  BorderThickness="0,0,3,3" 
  BorderBrush="{x:Static SystemColors.ControlDarkBrush}">
    <ContentPresenter ... />
</Border>
</Border>

This provides the added advantage of all the other features that border provides-- corner radius and such.

这提供了边框提供的所有其他功能的附加优势 - 角半径等。

回答by Micah

Honestly probably the easiest way would be to use layering techniques. For instance create a grid like this:

老实说,最简单的方法可能是使用分层技术。例如创建一个像这样的网格:

  <Grid Width="50" Height="50">  
     <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
     </Grid.ColumnDefinitions>

     <!-- Top Border -->
     <Border Height="3" Background="LightGray" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" />

     <!-- Right Border -->
     <Border Width="3" Background="DarkGray" Grid.Column="2" Grid.Row="0" Grid.RowSpan="3" />

     <!-- Content -->
     <Border Background="Gray" Grid.Row="1" Grid.Column="1" />

     <!-- Left Border -->
     <Border Width="3" Background="LightGray" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" />

     <!-- Bottom Border -->
     <Border Height="3" Background="DarkGray" Grid.Row="2" Grid.Column="1" />

  </Grid>

I think you get the idea. This is probably the easiest way of doing it. You could set this up as a template and use it like this:

我想你应该已经明白了。这可能是最简单的方法。您可以将其设置为模板并像这样使用它:

<Template x:Key="My3DBorder" TargetType="ContentControl">
    <!-- Put the Grid definition in here from above -->
</Template>

<ContentControl Template="{StaticResource My3dBorder}">
   <!-- My Content Goes Here -->
</ContentControl>

回答by Glen

you can reference the PresentationFramework.Classicassembly and then use

您可以参考PresentationFramework.Classic程序集,然后使用

<Window xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic">
...

<Grid Width="50" Height="50">
    <mwt:ClassicBorderDecorator BorderThickness="3,3,3,3"/>
</Grid>

I was using this technique for looking at control templatesto see how default buttons were styled

我正在使用这种技术来查看控件模板以查看默认按钮的样式

回答by LuckyLuke82

My two cents for cool approach that I prefer - using shadows too (as a style, so you can use It anywhere):

我更喜欢的酷方法的两美分 - 也使用阴影(作为一种样式,因此您可以在任何地方使用它):

  <Style x:Key="border_style" TargetType="{x:Type Border}">
        <Style.Resources>
            <DropShadowEffect x:Key="dropShadowEffect" BlurRadius="8" ShadowDepth="1" Color="#FF2686AA" RenderingBias="Quality"/>
         </Style.Resources>

        <Setter Property="CornerRadius" Value="2" />
        <Setter Property="Effect" Value="{StaticResource dropShadowEffect}"/>
        <Setter Property="BorderBrush" Value="Gray"/>
        <Setter Property="BorderThickness" Value="1.2,1.2,0.3,0.3"/>
    </Style>

Usage (when you register your styles):

用法(当您注册样式时):

<Border Style="{StaticResource border_style}">

 </Border>

Position of 3D border effect Is done with BorderThickness. And you can play with colors, ShadowDepth,BlurRadius, CornerRadius etc.

3D 边框效果的位置由 BorderThickness 完成。您可以使用颜色、ShadowDepth、BlurRadius、CornerRadius 等。

回答by Astrogator

Needed a classic 3D 'inset' border. Based on @GregD's answer (thank you!):

需要一个经典的 3D '插图' 边框。基于@GregD 的回答(谢谢!):

    <Border BorderThickness="0,0,1,1" BorderBrush="{x:Static SystemColors.ControlLightLightBrush}">
        <Border BorderThickness="1,1,0,0" BorderBrush="{x:Static SystemColors.ControlDarkBrush}">
            <Border BorderThickness="0,0,1,1" BorderBrush="{x:Static SystemColors.ControlLightBrush}">
                <Border BorderThickness="1,1,0,0" BorderBrush="{x:Static SystemColors.ControlDarkDarkBrush}">
                ..
                </Border>
            </Border>
        </Border>
    </Border>

To make it an 'outset', revert the order of 1st and 2nd element pairs.

要使其成为“开始”,请反转第 1 和第 2 元素对的顺序。