惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
美团技术团队
量子位
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
小众软件
小众软件
博客园 - 司徒正美
罗磊的独立博客
云风的 BLOG
云风的 BLOG
B
Blog RSS Feed
博客园 - 聂微东

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
winui3 自定义标题栏-zodream梦想开源/个人编程日记
zodream · 2023-11-20 · via zodream梦想开源/个人编程日记

winui3 自定义标题栏

MainWindow.xaml 添加

<Grid  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="48"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <!-- 标题栏 -->
    <Border x:Name="AppTitleBar" VerticalAlignment="Top">
        <TextBlock x:Name="AppTitle" Text="{StaticResource AppTitleName}" VerticalAlignment="Top" Margin="0,8,0,0" />
    </Border>
</Grid>

12345678910

MainWindow.xaml.cs 中添加

public MainWindow()
{
    this.InitializeComponent();

    // 自定义标题栏
    ExtendsContentIntoTitleBar = true;
    SetTitleBar(AppTitleBar);
}

12345678

左侧添加按钮

如果只是在左侧添加按钮,可以直接使用 xaml 定义

MainWindow.xaml 添加

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="48"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinHeight="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Content="返回" Click="BackBtn_Click"/>
        <!-- 标题栏 -->
        <Border x:Name="AppTitleBar" Grid.Column="1" VerticalAlignment="Top">
            <TextBlock x:Name="AppTitle" Text="{StaticResource AppTitleName}" VerticalAlignment="Top" Margin="0,8,0,0" />
        </Border>
    </Grid>
</Grid>

1234567891011121314151617

添加自定义可点击内容

如果需要在中间添加可点击可输入内容,那么就需要这样做

MainWindow.xaml 添加

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition MinHeight="48"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Border x:Name="AppTitleBar" VerticalAlignment="Top">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="AppTitle" Text="{StaticResource AppTitleName}" VerticalAlignment="Top" Margin="0,8,0,0" />
            <TextBox x:Name="SearchTb" Grid.Column="1" />
        </Grid>
    </Border>
</Grid>

1234567891011121314151617

MainWindow.xaml.cs 中添加

public MainWindow()
{
    this.InitializeComponent();

    // 自定义标题栏
    ExtendsContentIntoTitleBar = true;
    SetTitleBar(AppTitleBar);

    var _baseWindowHandle = WindowNative.GetWindowHandle(this);
    var windowId = Win32Interop.GetWindowIdFromWindow(_baseWindowHandle);
    var _appWindow = AppWindow.GetFromWindowId(windowId);
    var dpiScale = Content.XamlRoot.RasterizationScale;
    var nonClientSource = InputNonClientPointerSource.GetForWindowId(_appWindow.Id);

    var inputPoint = SearchTb.TransformToVisual(Content).TransformPoint(new Point(0, 0));
    nonClientSource.ClearRegionRects(NonClientRegionKind.Passthrough);
    nonClientSource.SetRegionRects(NonClientRegionKind.Passthrough, 
    // 可以添加多个区域
        [new(
            (int)Math.Round(inputPoint.X * dpiScale),
            (int)Math.Round(inputPoint.Y * dpiScale),
            (int)Math.Round(SearchTb.ActualWidth * dpiScale), // 请注意,要获取到可点击元素的显示尺寸才行
            (int)Math.Round(SearchTb.ActualHeight * dpiScale)
    )]);
}

12345678910111213141516171819202122232425

Command 绑定

使用 Command={TemplateBinding BackCommand} 可能无效

可以使用 Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=BackCommand}"

转载请保留原文链接: https://zodream.cn/blog/id/248.html