You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI如何仅将Border控件的顶部设置为透明?

.NET MAUI 实现Border顶部透明、其余边可见的方案

目前.NET MAUI的Border控件没有内置单独设置单条边透明度的属性,你可以通过以下几种方案实现需求:

方案一:Grid嵌套BoxView模拟边框

这是最直接的实现方式,用Grid承载内容,然后在左、下、右三个方向添加BoxView作为可见边框,顶部不添加即可:

<Grid x:Name="borderGrid" Padding="0">
    <!-- 你的内容控件,注意设置Padding避免被边框遮挡 -->
    <Label Text="测试内容" Padding="10" />
    
    <!-- 左侧边框 -->
    <BoxView WidthRequest="1" 
             BackgroundColor="#333333" 
             HorizontalOptions="Start" 
             VerticalOptions="Fill" />
    <!-- 底部边框 -->
    <BoxView HeightRequest="1" 
             BackgroundColor="#333333" 
             VerticalOptions="End" 
             HorizontalOptions="Fill" />
    <!-- 右侧边框 -->
    <BoxView WidthRequest="1" 
             BackgroundColor="#333333" 
             HorizontalOptions="End" 
             VerticalOptions="Fill" />
</Grid>

你可以根据需求调整BoxView的WidthRequest/HeightRequest(边框粗细)和BackgroundColor(边框颜色)。

方案二:使用Path绘制自定义边框

如果需要更灵活的边框样式(比如圆角),可以用Path绘制左、下、右三条边:

<Grid x:Name="pathGrid" Padding="0">
    <Path Stroke="#333333" 
          StrokeThickness="1"
          StrokeLineJoin="Round"> <!-- 可选:设置边角圆滑 -->
        <Path.Data>
            <GeometryGroup>
                <!-- 左侧边:从左上角到左下角 -->
                <LineGeometry StartPoint="0,0" 
                              EndPoint="0,{Binding Source={x:Reference pathGrid}, Path=Height}" />
                <!-- 底部边:从左下角到右下角 -->
                <LineGeometry StartPoint="0,{Binding Source={x:Reference pathGrid}, Path=Height}" 
                              EndPoint="{Binding Source={x:Reference pathGrid}, Path=Width},{Binding Source={x:Reference pathGrid}, Path=Height}" />
                <!-- 右侧边:从右下角到右上角 -->
                <LineGeometry StartPoint="{Binding Source={x:Reference pathGrid}, Path=Width},{Binding Source={x:Reference pathGrid}, Path=Height}" 
                              EndPoint="{Binding Source={x:Reference pathGrid}, Path=Width},0" />
            </GeometryGroup>
        </Path.Data>
    </Path>
    
    <!-- 内容控件 -->
    <Label Text="带圆角的边框测试" Padding="10" />
</Grid>

如果需要圆角,可以给Path的StrokeLineJoin设置为Round,或者调整LineGeometry的起点/终点坐标适配圆角。

方案三:封装自定义控件(复用场景)

如果需要在多个页面复用这个效果,可以把上述Grid/Path的逻辑封装成自定义控件,方便后续调用:

public class PartialBorder : Grid
{
    public static readonly BindableProperty BorderColorProperty =
        BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(PartialBorder), Color.Black);

    public static readonly BindableProperty BorderThicknessProperty =
        BindableProperty.Create(nameof(BorderThickness), typeof(double), typeof(PartialBorder), 1.0);

    public Color BorderColor
    {
        get => (Color)GetValue(BorderColorProperty);
        set => SetValue(BorderColorProperty, value);
    }

    public double BorderThickness
    {
        get => (double)GetValue(BorderThicknessProperty);
        set => SetValue(BorderThicknessProperty, value);
    }

    public PartialBorder()
    {
        var leftBorder = new BoxView();
        leftBorder.SetBinding(BoxView.BackgroundColorProperty, new Binding(nameof(BorderColor), source: this));
        leftBorder.SetBinding(BoxView.WidthRequestProperty, new Binding(nameof(BorderThickness), source: this));
        leftBorder.HorizontalOptions = LayoutOptions.Start;
        leftBorder.VerticalOptions = LayoutOptions.Fill;

        var bottomBorder = new BoxView();
        bottomBorder.SetBinding(BoxView.BackgroundColorProperty, new Binding(nameof(BorderColor), source: this));
        bottomBorder.SetBinding(BoxView.HeightRequestProperty, new Binding(nameof(BorderThickness), source: this));
        bottomBorder.VerticalOptions = LayoutOptions.End;
        bottomBorder.HorizontalOptions = LayoutOptions.Fill;

        var rightBorder = new BoxView();
        rightBorder.SetBinding(BoxView.BackgroundColorProperty, new Binding(nameof(BorderColor), source: this));
        rightBorder.SetBinding(BoxView.WidthRequestProperty, new Binding(nameof(BorderThickness), source: this));
        rightBorder.HorizontalOptions = LayoutOptions.End;
        rightBorder.VerticalOptions = LayoutOptions.Fill;

        Children.Add(leftBorder);
        Children.Add(bottomBorder);
        Children.Add(rightBorder);

        // 添加内容容器
        var contentPresenter = new ContentPresenter();
        contentPresenter.Padding = new Thickness(BorderThickness, 0, BorderThickness, BorderThickness);
        this.SetBinding(ContentPresenter.ContentProperty, new Binding(nameof(Content), source: this));
        Children.Add(contentPresenter);
    }
}

使用时直接在XAML中引用:

<local:PartialBorder BorderColor="#333333" BorderThickness="2">
    <Label Text="自定义控件测试" />
</local:PartialBorder>

内容的提问来源于stack exchange,提问作者Alpay Çalışır

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 19:50:19