![](/img/trans.png)
[英]Can't iterate through cells in a C# DataGrid to change their background colour
[英]WinUI 3 Can't properly change the background colour of the titlebar
我的目標是使標題欄的背景顏色和應用程序內的內容顏色相同,但這是行不通的。
我想把所有東西都變成這種顏色#FF2B2B2B,問題是雖然應用程序的內容顏色正確,但標題欄的顏色比應該的顏色淺,事實上默認的 XAML 顏色也會出現同樣的問題黑色,但使用默認的 XAML 紅色一切正常。
主窗口.xaml
<Window
x:Class="TItleBarBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TItleBarBackground"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="Colour" Color="#FF2B2B2B"/>
</Grid.Resources>
<Grid x:Name="AppTitleBar" Height="28" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="{StaticResource Colour}">
<TextBlock Text="Title"/>
</Grid>
<Rectangle Fill="{StaticResource Colour}" Margin="0,40,0,0" />
</Grid>
MainWindow.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace TItleBarBackground
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
}
}
}
不幸的是,目前存在一個已知問題(錯誤)。
當他們解決問題時,您需要覆蓋這些:
WindowCaptionBackground
WindowCaptionBackgroundDisabled
這是代碼。
申請.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<Color x:Key="AppCommonBackground">#FF2B2B2B</Color>
<SolidColorBrush x:Key="WindowCaptionBackground" Color="{StaticResource AppCommonBackground}"/>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled" Color="{StaticResource AppCommonBackground}"/>
</ResourceDictionary>
</Application.Resources>
主窗口.xaml
<Window
x:Class="TitleBars.MainWindow"
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:local="using:TitleBars"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid RowDefinitions="Auto,*" Background="{StaticResource AppCommonBackground}">
<Grid
x:Name="AppTitleBar"
Grid.Row="0"
Height="28"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Canvas.ZIndex="1">
<TextBlock Text="Title" />
</Grid>
<TextBlock
Grid.Row="1"
Text="Body" />
</Grid>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.