Search code examples
c#wpfxamlwpf-controlswpf-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. 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. I'm not good in 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. 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.

   <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


Solution

  • 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.