簡體   English   中英

網格可從多個網格中看到

[英]Grid visible from multiple grids

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

        }
    }
}

當我點擊按鈕時,我想打開一個紅色的網格。

從您的代碼后面刪除GridButton對象(C#),因為您已經在XAML中擁有它們。

MainWindow類是一個部分類,分布在您的C#代碼和XAML的編譯輸出中。 所以你仍然可以從你的C#代碼中引用RGrid等,就像你在同一個文件中聲明它一樣。

它現在應該適合你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM