0

i am learning WPF here is my XAML.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="634">
<StackPanel>
    <Button Height="35"
            Width="89"
            Name="p1">Hello</Button>
    <Border CornerRadius="5"
            BorderThickness="1"
            BorderBrush="Black"
            Height="35"
            Width="254"
            Margin="91,192,150,79">
        <TextBox HorizontalAlignment="Left"
                 VerticalAlignment="Center"
                 Background="Transparent"
                 BorderThickness="0"
                 Height="35"
                 Width="250"
                 Name="txtContents" />
    </Border>
    <Button Height="23"
            Name="button1"
            Width="75">Button</Button>
</StackPanel>

button textbox is showing but the problem is I am not being able to drag the control to another location. how to fix it. please help. thanks

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

2

If you mean you can't drag the Button controls to different locations, it's because they're contained within a StackPanel - stacking them one on top of each other.

If you change that StackPanel to be a Grid, you'd have the ability to drag it around in a canvas-like manner.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="634">
<Grid>
    <Button Height="35"
            Width="89"
            Name="p1">Hello</Button>
    <Border CornerRadius="5"
            BorderThickness="1"
            BorderBrush="Black"
            Height="35"
            Width="254"
            Margin="91,192,150,79">
        <TextBox HorizontalAlignment="Left"
                 VerticalAlignment="Center"
                 Background="Transparent"
                 BorderThickness="0"
                 Height="35"
                 Width="250"
                 Name="txtContents" />
    </Border>
    <Button Height="23"
            Name="button1"
            Width="75">Button</Button>
</Grid>

This question may shed some light on where to use Grids and StackPanels.

Community
  • 1
  • 1
Daniel May
  • 8,156
  • 1
  • 33
  • 43
1

If by "able to drag the control to another location" you are talking about repositioning the control using Expression Blend or the Visual Studio Designer you need to change the StackPanel to a Grid

So it would become-

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="634">
<Grid>
    <Button Height="35"
            Width="89"
            Name="p1">Hello</Button>
    <Border CornerRadius="5"
            BorderThickness="1"
            BorderBrush="Black"
            Height="35"
            Width="254"
            Margin="91,192,150,79">
        <TextBox HorizontalAlignment="Left"
                 VerticalAlignment="Center"
                 Background="Transparent"
                 BorderThickness="0"
                 Height="35"
                 Width="250"
                 Name="txtContents" />
    </Border>
    <Button Height="23"
            Name="button1"
            Width="75">Button</Button>
</Grid>
Jason Quinn
  • 2,443
  • 3
  • 28
  • 36