在我使用代码隐藏的 WPF 应用程序中,我正在使用边框创建一些卡片,在其中添加几个元素,并且我试图使第一行的文本左对齐,按钮右对齐,这就是结果我正在寻找:
但我无法以任何方式做到这一点,我尝试过使用网格或 DockPanels 的其他方法,但我也无法将按钮正确地向右对齐。这是我得到的结果:
Este el código que estoy usando:
Border border = new Border
{
Width = 290,
Height = 130,
Margin = new System.Windows.Thickness(10, 10, 0, 0),
CornerRadius = new CornerRadius(5),
BorderThickness = new System.Windows.Thickness(1),
BorderBrush = Brushes.Transparent,
Background = new SolidColorBrush(Color.FromArgb(255, 47, 48, 48)),
Tag = a.Id
};
border.MouseLeftButtonUp += MyBorder_MouseLeftButtonUp;
StackPanel stackPanelV = new StackPanel
{
Orientation = Orientation.Vertical
};
StackPanel stackPanelL1 = new StackPanel
{
Orientation = Orientation.Horizontal
};
TextBlock textBlockL1 = new TextBlock
{
Text = "Text line 1",
Margin = new System.Windows.Thickness(12, 10, 0, 0),
HorizontalAlignment = HorizontalAlignment.Left,
FontWeight = FontWeights.Bold,
FontSize = 16
};
ToggleSwitch toggle = new ToggleSwitch
{
IsOn = true,
Margin = new System.Windows.Thickness(0, 5, 0, 0),
HorizontalAlignment = HorizontalAlignment.Right,
OnContent = null,
OffContent = null
};
toggle.Toggled += TsActivo_Toggled;
stackPanelL1.Children.Add(textBlockL1);
stackPanelL1.Children.Add(toggle);
TextBlock textBlockL2 = new TextBlock
{
Text = "Text line 2",
Margin = new System.Windows.Thickness(12, 0, 0, 0),
FontSize = 12,
Foreground = Brushes.LightGray
};
TextBlock textBlockL3 = new TextBlock
{
Text = "Text line 3",
Margin = new System.Windows.Thickness(12, 5, 0, 0),
FontSize = 12,
Foreground = Brushes.LightGray
};
TextBlock textBlockL4 = new TextBlock
{
Text = "Text line 4",
Margin = new System.Windows.Thickness(12, 5, 0, 0),
FontSize = 12,
Foreground = Brushes.LightGray
};
stackPanelV.Children.Add(stackPanelL1);
stackPanelV.Children.Add(textBlockL2);
stackPanelV.Children.Add(textBlockL3);
stackPanelV.Children.Add(textBlockL4);
border.Child = stackPanelV;
contenedorFichas.Children.Add(border);
非常感谢您的帮助
用 Grid 替换 StackPanel。正如 emoacht 在评论中所说,StackPanels 不适合布局填充可用空间的元素。