C# WPF新手,我将如何在我的表单中创建这些彩色条?

C# WPF新手,我将如何在我的表单中创建这些彩色条?,c#,wpf,grid,background-color,C#,Wpf,Grid,Background Color,我正试图抛弃Windows窗体,从现在开始学习专业地使用WPF。上图是一个在Windows窗体中完成的窗体,我想在WPF中重新创建它 以下是我目前拥有的XAML: <Window x:Class="PizzaSoftware.UI.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com

我正试图抛弃Windows窗体,从现在开始学习专业地使用WPF。上图是一个在Windows窗体中完成的窗体,我想在WPF中重新创建它

以下是我目前拥有的XAML:

<Window x:Class="PizzaSoftware.UI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="297" Width="466" >
    <Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".20*"/>
            <ColumnDefinition />
            <ColumnDefinition Width=".20*"/>    
        </Grid.ColumnDefinitions>     
        <Rectangle Grid.Column="0">
            <Backcolor?
        </Rectangle>
    </Grid>
</Window>


是的,你的方法很好。使用
Fill
属性设置背景色:

            <Rectangle Grid.Column="0" Fill="Orange" />
            <Rectangle Grid.Column="2" Fill="Orange" />


将窗口背景设置为橙色。然后,将栅格的背景设置为白色,设置栅格的宽度,使其在任意一侧留有空间,并将栅格水平对齐设置为中心

<Windows ....
    Background="Orange>
  <Grid Background="White" HorizontalAlignment="Center" Width="300">
  ...
  </Grid>
</Window>

我会跳过矩形,在网格上设置背景颜色,然后在网格内的组件上设置颜色

<Grid Background="Orange">


< /代码> 您可以将网格的<代码>背景<代码>属性设置为橙色,然后将控件添加到中间列中,并将其<代码>背景属性设置为白色。下面是一个示例代码:

<Window x:Class="PizzaSoftware.UI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="297" Width="466" 
        Height="297"
        Width="466">
    <Grid ShowGridLines="True" Background="Orange">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".20*"/>
            <ColumnDefinition />
            <ColumnDefinition Width=".20*" />
        </Grid.ColumnDefinitions>

        <!--Control with white background in second column-->
        <GroupBox Background="White" Grid.Column="1">
            <!-- Groupbox content here-->
        </GroupBox>
    </Grid>
</Window>


因为这是默认值,所以不需要对齐。非常好的点,+1。对于刚加入WPF的人来说,这是一件非常重要的事情,所以我会留下它。或者你可以和@Joel Lucsy一起把它放在窗户上,这可能更适合您。您还可以将整个背景设置为橙色,将中间列内容的背景设置为白色。将矩形添加到网格中实际上会将更多元素添加到可视化树中。虽然这个例子一开始并不复杂,但我一直主张在可视化树中使用较少的元素。
<Window x:Class="PizzaSoftware.UI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="297" Width="466" 
        Height="297"
        Width="466">
    <Grid ShowGridLines="True" Background="Orange">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".20*"/>
            <ColumnDefinition />
            <ColumnDefinition Width=".20*" />
        </Grid.ColumnDefinitions>

        <!--Control with white background in second column-->
        <GroupBox Background="White" Grid.Column="1">
            <!-- Groupbox content here-->
        </GroupBox>
    </Grid>
</Window>