简体   繁体   English

无法在C#中向GRID面板添加其他控件

[英]Cannot add different controls to GRID panel in C#

I'm completely new to XAML and WPF and I cannot add other type of control (like TextBox or Label) to grid after adding a button. 我是XAML和WPF的新手,添加按钮后无法将其他类型的控件(如TextBox或Label)添加到网格中。 Could you please explain me where do I make a mistake (I guess it should be some simple mistake by a newbie guy). 您能给我解释我在哪里犯错(我想这应该是一个新手的简单错误)。

I've created an app in C# using Win Forms and decided to create the same app using WPF, which I never used before, so that I can learn something new. 我已经使用Win Forms在C#中创建了一个应用程序,并决定使用WPF创建相同的应用程序,这是我以前从未使用过的,以便可以学到一些新知识。 I'm not good in C# FYI. 我在C#FYI中不好。 I started new project with WPF application in Visual Studio, created 2 columns and 4 rows for a grid and started adding some controls to it. 我在Visual Studio中使用WPF应用程序开始了新项目,为网格创建了2列和4行,并开始向其中添加一些控件。 Unfortunately, I cannot make a working app with buttons and another type of control on same grid. 不幸的是,我无法在同一个网格上使用按钮和其他类型的控件来制作可运行的应用程序。 It looks like grid would allow me to add only 1 type of controls, which for me makes no sense and I couldn't find any information about such restriction. 看起来网格将使我只能添加一种控件类型,这对我来说没有任何意义,并且我找不到有关此类限制的任何信息。 Simple XAML code included. 包含简单的XAML代码。

   <Window x:Class="AjStock_WPF_3.CSVToSQL"
    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:AjStock_WPF_3"
    mc:Ignorable="d"
    Title="CSVToSQL" Height="450" Width="800" MinHeight="200" MinWidth="250">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="120" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>

    <Button Grid.Column="0" Grid.Row="0">
        <Button.Name>GetCSVFile</Button.Name>
        <Button.Content>Get CSV file</Button.Content>
        <Button.Margin>10</Button.Margin>
        <Button.VerticalContentAlignment>Center</Button.VerticalContentAlignment>
        <Button.HorizontalContentAlignment>Center</Button.HorizontalContentAlignment>
        <Button.FontWeight>Bold</Button.FontWeight>
        <Button.FontSize>14</Button.FontSize>
        <Button.BorderThickness>2</Button.BorderThickness>
    </Button>

    <TextBox Grid.Column="1" Grid.Row="0">
        <TextBox.VerticalContentAlignment>Center</TextBox.VerticalContentAlignment>
        <TextBox.HorizontalContentAlignment>Center</TextBox.HorizontalContentAlignment>
        <TextBox.Margin>10</TextBox.Margin>
    </TextBox>

</Grid>

Expected result would be a window with controls like this: 预期结果将是带有以下控件的窗口:

Button | TextBox  
Label | TextBox  
Button | TextBox  
Button | Button

Error that I'm receiving: 我收到的错误:

CS0029 Cannot implicitly convert type 'System.Windows.Controls.TextBox' to 'System.Windows.Controls.Button' AjStock_WPF_3 D:\\OneDrive\\Microsoft Visual Studio\\source\\repos\\AjStock_WPF_3\\AjStock_WPF_3\\obj\\Debug\\CSVToSQL.g.cs 104 Active CS0029无法将类型'System.Windows.Controls.TextBox'隐式转换为'System.Windows.Controls.Button'AjStock_WPF_3 D:\\ OneDrive \\ Microsoft Visual Studio \\ source \\ repos \\ AjStock_WPF_3 \\ AjStock_WPF_3 \\ obj \\ Debug \\ CSVToSQL.g。 cs 104活动

You need to remove 您需要删除

<Button.Name>GetCSVFile</Button.Name>

and replace with 并替换为

<Button Grid.Column="0" Grid.Row="0" Name="GetCSVFile">

Not sure why this is required as when the textbox is removed, leaving just the button, it compiles. 不确定为什么要这样做,因为删除文本框时仅留下按钮即可编译。 It's probably related to the mapping of x:Name but not 100% sure. 它可能与x:Name的映射有关,但不是100%确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM