网格可从多个网格中看到

时间:2016-08-05 13:29:50

标签: c# wpf

我是wpf的新手,我尝试通过以下代码段点击按钮click.go上的相应网格, 这是我的XMAL代码。

<Window x:Class="SampleWpf.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:SampleWpf"
        mc:Ignorable="d"
        Title="SamlpplePage" Height="350" Width="525">
    <Grid Background="Black">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid  Grid.Column="0" Margin="10,10,281,52">

                <Grid Background="#666666" Margin="10,10,-220,10">
                    <StackPanel Orientation="Vertical" Margin="10">
                    <Button 
                            Content="Red"
                            Name="Rbutton"
                            Height="35" 
                            FontSize="20" 
                            TextElement.Foreground="Red" 
                            Click="RedClick">
                    </Button>
                    <Button 
                            Content="Blue"
                            Name="Bbutton"  
                            Height="35" 
                            FontSize="20" 
                            TextElement.Foreground="Blue"
                            Click="BlueClick">
                    </Button>
                    <Button 
                            Content="Green"
                            Name="Gbutton"
                            Height="35"
                            FontSize="20" 
                            TextElement.Foreground="Green"
                            Click="GreenClick">
                    </Button>
                    <Button 
                            Content="White"
                            Name="Wbutton"
                            Height="35" 
                            FontSize="20" 
                            TextElement.Foreground="White"
                            Click="WhiteClick">
                    </Button>
                    <Button
                            Content="Yellow"
                            Name="Ybutton"
                            Height="35"
                            FontSize="20" 
                            TextElement.Foreground="Yellow"
                            Click="YellowClick" >
                    </Button>
                    <Button
                            Content="Pink"
                            Name="Pbutton"
                            Height="35" 
                            FontSize="20" 
                            TextElement.Foreground="Pink"
                            Click="PinkClick">
                      </Button>
                    </StackPanel>
                </Grid>


        </Grid>

        <Grid Grid.Column="1"  Margin="10">
            <Grid Background="#666666"  ></Grid>
            <Grid 
                Background="Red" 
                Margin="10"
                Name="RGrid">
            </Grid>

            <Grid 
                Background="Blue" 
                Margin="10"
                Name="BGrid">
            </Grid>

            <Grid 
                Background="Green" 
                Margin="10"
                Name="GGRid">
            </Grid>

            <Grid
                Background="White" 
                Margin="10"
                Name="WGrid">
            </Grid>

            <Grid 
                Background="Yellow"
                Margin="10"
                Name="YGrid">
            </Grid>

            <Grid 
                Background="Pink" 
                Margin="10"
                Name="PGrid">
            </Grid
        </Grid>
   </Grid>
</Window>

这是我的C#代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SampleWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Button RButton, GButton, BButton, WButton, YButton, PButton;
            Grid RGrid,GGrid, BGrid, WGrid, YGrid, PGrid;
        }
        public void RedClick(object sender, RoutedEventArgs e)
        {

            RGrid.Visibility = Visibility.Visible;
        }
        public void GreenClick(object sender, RoutedEventArgs e)
        {

        }
        public void BlueClick(object sender, RoutedEventArgs e)
        {

        }
        public void WhiteClick(object sender, RoutedEventArgs e)
        {

        }
        public void YellowClick(object sender, RoutedEventArgs e)
        {

        }
        public void PinkClick(object sender, RoutedEventArgs e)
        {

        }
    }
}

当我点击按钮时,我想打开一个红色的网格。

1 个答案:

答案 0 :(得分:0)

从您的代码中删除GridButton对象(C#),因为您已经在XAML中拥有它们。

MainWindow类是一个部分类,它分布在您的C#代码和XAML的编译输出中。因此,您仍然可以从C#代码中引用RGrid等,就好像您已在同一文件中声明它一样。

它应该适合你。