我在堆栈面板和内部格式化方面存在问题, 我正在研究咖啡吧的小应用可以说,客户可以从屏幕中间选择饮料和 饮料将在我的堆叠面板中显示在屏幕的右侧,这就是现在的样子(当客户选择某件东西 - 可以说可乐时): 如何重新组织我的堆栈面板WPF
我的代码,一切都完成了:
public DrinksPanel(int id, int groupID, double price,double priceHappyHour, string text, int fontSize, int numberSize, int buttonWidth, int buttonHeight, int margin, Thickness border, byte[] image)
: base()
{
this.Width = buttonWidth+90;
this.Height = buttonHeight + 35;
image.Source = GetImageFromByte(image);
ID = id;
groupID = groupID;
price = price;
priceHappyHour = priceHappyHour;
LeftPanelButton.HorizontalAlignment = HorizontalAlignment.Center;
LeftPanelButton.VerticalAlignment = VerticalAlignment.Center;
LeftPanelButton.Orientation = Orientation.Vertical;
LeftPanelButton.MaxWidth = LeftPanelButton.Width = buttonWidth;
LeftPanelButton.Height = LeftPanelButton.MaxHeight = this.Height;
LeftPanelButton.Background = new SolidColorBrush(Colors.Red);
_image.Width = LeftPanelButton.Width;
_image.Height = buttonHeight;
_image.HorizontalAlignment = HorizontalAlignment.Left;
_image.VerticalAlignment = VerticalAlignment.Center;
PanelQtyNumber.HorizontalAlignment = HorizontalAlignment.Right;
PanelQtyNumber.VerticalAlignment = VerticalAlignment.Center;
PanelQtyNumber.Orientation = Orientation.Horizontal;
PanelQtyNumber.Width = PanelQtyNumber.MaxWidth = this.Width - LeftPanelButton.Width;
DrinkNameLabel.VerticalContentAlignment = VerticalAlignment.Center;
DrinkNameLabel.HorizontalContentAlignment = HorizontalAlignment.Left;
DrinkNameLabel.FontSize = fontSize;
DrinkNameLabel.Foreground = System.Windows.Media.Brushes.White;
DrinkNameLabel.MaxWidth = _image.Width;
DrinkNameLabel.FontFamily = Util.Font;
NumberLabel.FontSize = numberSize;
NumberLabel.Margin = new Thickness(margin, margin, 0, margin);
NumberLabel.VerticalAlignment = VerticalAlignment.Center;
NumberLabel.HorizontalAlignment = HorizontalAlignment.Right;
NumberLabel.HorizontalContentAlignment = HorizontalAlignment.Right;
NumberLabel.Foreground = System.Windows.Media.Brushes.White;
NumberLabel.FontWeight = FontWeights.Bold;
NumberLabel.Background = this.Background;
How to reorganize my stack panel WPF
DrinkNameLabel.Content = text;
NumberLabel.Content = "x1";
LeftPanelButton.Children.Add(image);
LeftPanelButton.Children.Add(DrinkNameLabel);
PanelQtyNumber.Children.Add(NumberLabel);
Rectangle rct = new Rectangle();
rct.Fill = System.Windows.Media.Brushes.White;
rct.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
rct.Height = 2;
_animation = new DoubleAnimation(numberSize, numberSize + 15, new Duration(new TimeSpan(0, 0, 0, 0, 300)));
_animation.AutoReverse = true;
this.Margin = border;
this.Orientation = Orientation.Horizontal;
StackPanel spNew = new StackPanel();
spNew.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
spNew.Orientation = System.Windows.Controls.Orientation.Horizontal;
spNew.Children.Add(LeftPanelButton);
spNew.Children.Add(PanelQtyNumber);
StackPanel spNew2 = new StackPanel();
spNew2.Orientation = System.Windows.Controls.Orientation.Vertical;
spNew2.Children.Add(spNew);
this.Children.Add(spNew2);
}
我不能动我的文字一样,正确的或点儿:/任何人都可以帮我这个吧。
你的问题的宽度是你想要一个水平对齐而不是垂直于您的项目定位。但不要这样做,检查模板和你的xaml的UI不是c#... – gmetax
更改spNew2.Orientation = System.Windows.Controls.Orientation.Vertical; 与水平线 – gmetax
你有任何xaml?你的UI代码真的不应该在C#中设置。对于这种情况,您应该使用xaml中的ItemsControl和数据模板,以及C#中的ObservableCollection。 – Joe