WPF控件定位问题

发布于 2024-10-14 02:41:37 字数 1052 浏览 2 评论 0原文

我正在学习 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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-10-21 02:41:37

如果您的意思是无法将 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>

这个问题可能会揭示在哪里使用Grid和StackPanel。

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.

假装爱人 2024-10-21 02:41:37

如果“能够将控件拖动到另一个位置”,您正在讨论使用 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文