2016-11-29 76 views
-1

我在堆栈面板和内部格式化方面存在问题, 我正在研究咖啡吧的小应用可以说,客户可以从屏幕中间选择饮料和 饮料将在我的堆叠面板中显示在屏幕的右侧,这就是现在的样子(当客户选择某件东西 - 可以说可乐时): enter image description here如何重新组织我的堆栈面板WPF

这就是我想如何让它看起来像像:enter image description here

我的代码,一切都完成了:

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); 
     } 

我不能动我的文字一样,正确的或点儿:/任何人都可以帮我这个吧。

+0

你的问题的宽度是你想要一个水平对齐而不是垂直于您的项目定位。但不要这样做,检查模板和你的xaml的UI不是c#... – gmetax

+0

更改spNew2.Orientation = System.Windows.Controls.Orientation.Vertical; 与水平线 – gmetax

+4

你有任何xaml?你的UI代码真的不应该在C#中设置。对于这种情况,您应该使用xaml中的ItemsControl和数据模板,以及C#中的ObservableCollection。 – Joe

回答

1

更改您的LeftPanelButton方向为水平,然后文本将移动到图像的右侧。

LeftPanelButton.Orientation = Orientation.Horizontal; 

如果你不想在你的文本中的红色背景,然后删除LeftPanelButton背景。

NumberLabel设置你想要的喜欢的高度和宽度80,设置背景是你的绿色和前景是黑色的。

如果还是文本是不可见的再调整像

_image.Width = LeftPanelButton.Width - 100; 
+0

我会尽快尝试,我会给你答复。 –

+0

我试过,但之后,我看不到我的文章的文本.. –

+0

其宽度的问题,你想要根据要求调整您的图像宽度 现在我给予例如:_image.Width = LeftPanelButton.Width - 100; – Poovizhi