从 WPF c# 中的代码更改背景颜色

问题描述 投票:0回答:1

我的 .xaml 代码中有这个,并定义了背景:

<Window x:Class="BDE.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BDE"
        mc:Ignorable="d"
        Title="BDE" Height="768" Width="1024"
        Background="#FF1B1534"
        FocusManager.FocusedElement="{Binding ElementName=txt_identifikationsnummer}"
        WindowState="Maximized" ResizeMode="CanResize" WindowStartupLocation="CenterScreen">

我如何访问.cs代码中的背景属性以在函数中更改它

c# wpf xaml colors background
1个回答
0
投票

如果通过 .cs 代码,您的意思是

Window
后面的代码,那么您可以更改
Background
颜色,例如在
MainWindow
的构造函数内,如下所示

public MainWindow()
{
    InitializeComponent();
    Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
}

或者在 MainWindow.xaml.cs 中的任何方法中

© www.soinside.com 2019 - 2024. All rights reserved.