2016-08-05 129 views
1

我是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) 
     { 

     } 
    } 
} 

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

回答

0

从您的代码背后(C#)中删除GridButton对象,因为您的XAML中已包含它们。

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

它现在应该适合你。