none
wpf border的边框渐变 RRS feed

  • 问题

  • 请问怎样把上图的两个渐变色组合起来,形成一个四个边框都是渐变色的border啊。

    我知道可以让UI使用PS画一个,但是更希望使用WPF自有的东西解决他。

    2018年4月25日 2:16

答案

  • 你好,

    可以使用VisualBrush 来组合两个LinearGradientBrush, 下面的代码,你可以参考一下。

    <Window x:Class="ColeWPFSample.BorderSample.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ColeWPFSample.BorderSample"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <Window.Resources>
                <ResourceDictionary>
                <LinearGradientBrush x:Key="UnderBrush" EndPoint="0,1">
                    <GradientStop Color="#FFFF0000" Offset="0" />
                    <GradientStop Color="#00FF0000" Offset="1" />
                </LinearGradientBrush>
    
                <LinearGradientBrush x:Key="OverBrush" EndPoint="1,0">
                    <GradientStop Color="#00000000" Offset="0" />
                    <GradientStop Color="#FF000000" Offset="1" />
                </LinearGradientBrush>
    
                <VisualBrush x:Key="CombinedBrush">
                    <VisualBrush.Visual>
                        <Border Background="{StaticResource UnderBrush}">
                            <Rectangle Fill="{StaticResource OverBrush}" Width="1" Height="1" />
                        </Border>
                    </VisualBrush.Visual>
                </VisualBrush>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <Border Name="Rect" Height="200" Width="200" Background="{StaticResource ResourceKey=CombinedBrush}" />
        </Grid>
    </Window>
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月25日 8:39