在C#中动态填充矩形

时间:2017-01-25 01:14:34

标签: c# wpf

我在WPF中制作了一个模拟2D魔方的程序。在表单窗口中,我添加了代表六个边中的每一个的矩形,并根据它们的边来命名它们。问题是,我无法在任何方块上显示颜色。

private void Main()
    {
        switch (cubeL[0,0])
        {
            case "Blue":
                //var converter = new System.Windows.Media.BrushConverter();
                //var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FF00008B");
                L1.Fill = new SolidColorBrush(Colors.DarkBlue);
                break;

            case "Orange":
                //var converter1 = new System.Windows.Media.BrushConverter();
                //var brush1 = (System.Windows.Media.Brush)converter1.ConvertFromString("#FFFFA500");
                L1.Fill = new SolidColorBrush(Colors.Orange);
                break;
        }
    }

我正试图让橙色显示在一边,但我无法让它工作,而且我已经阅读了您使用的

rectanglename.Fill(Color)

所以我认为这是我的XAML的一个问题:

<Window x:Class="MonoBrick.Window1"
    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"
    mc:Ignorable="d"
    Title="Cube" Height="611.767" Width="624.5" ResizeMode="CanMinimize" WindowStyle="ThreeDBorderWindow">
<Grid RenderTransformOrigin="0.5,0.5">

    <Rectangle x:Name="L6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="91,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="7,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42" AutomationProperties.Name="L1"/>
    <Rectangle x:Name="L4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="7,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="7,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="91,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="49,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="49,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="49,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="L9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="91,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="139,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="181,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="356,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="272,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="272,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="272,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="356,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="314,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="314,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="314,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="R9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="356,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="488,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="404,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="404,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="404,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="488,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="446,172,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="446,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="446,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="B9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="488,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,132,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="F9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="223,212,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,45,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,45,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="139,85,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,5,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,45,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,5,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="181,85,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,5,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="U9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="223,85,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D6" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,297,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D4" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,297,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D7" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="139,337,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D3" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="223,258,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D5" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,297,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D2" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="181,258,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D8" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="181,337,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D1" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="39" Margin="139,258,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
    <Rectangle x:Name="D9" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="40" Margin="223,337,0,0" Stroke="Black" VerticalAlignment="Top" Width="42"/>
</Grid>

也许我只需要更改命名空间,或修复一个非常愚蠢的错误,我还在学习C#,所以任何帮助都会非常感激。

0 个答案:

没有答案