2016-08-05 6 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

XAMLにコードが既にあるため、GridおよびButtonオブジェクトをコードの背後(C#)から削除します。

MainWindowクラスは、C#コードとXAMLのコンパイル済みの出力にまたがって配置されている部分クラスです。したがって、同じファイルで宣言したかのように、C#コードからRGridなどを参照することはできます。

これで動作するはずです。

関連する問題