gpt4 book ai didi

c# - WPF - 在文本框旁边动态添加按钮

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:55 26 4
gpt4 key购买 nike

我正在动态创建标签、文本框和按钮。我需要 Button 与其右侧的文本框出现在同一行中。

这是我使用的代码:

    Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};

TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};

Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left
Margin = new Thickness(tb.ActualWidth),
};

我将按钮边距分配给文本框的右侧,但它仍然出现在文本框下的下一行中。

我做错了什么?

最佳答案

您可以使用StackPanel 来解决您的问题:

StackPanel spMain = new StackPanel() { Orientation = Orientation.Vertical };

Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};

StackPanel spInner = new StackPanel() { Orientation = Orientation.Horizontal };

TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};

Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(tb.ActualWidth),
};

spInner.Children.Add(tb);
spInner.Children.Add(btn);

spMain.Children.Add(lbl);
spMain.Children.Add(spInner);

您可以查看以下链接以获取更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.orientation.aspx

关于c# - WPF - 在文本框旁边动态添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com