WPF控制定位问题

时间:2011-01-24 10:02:34

标签: wpf wpf-controls

我正在学习WPF,这是我的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>

按钮文本框正在显示,但问题是我无法将控件拖到另一个位置。如何解决它。请帮忙。感谢

2 个答案:

答案 0 :(得分:1)

如果通过“能够将控件拖动到另一个位置”,您正在讨论使用Expression Blend或Visual Studio Designer重新定位控件,则需要将StackPanel更改为Grid

所以它会变成 -

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

答案 1 :(得分:1)

如果您的意思是无法将Button控件拖到不同的位置,那是因为它们包含在StackPanel中 - 将它们叠加在一起。

如果您将StackPanel更改为Grid,则可以以类似画布的方式将其拖动。

<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可以阐明在何处使用GridStackPanel s。