2016-11-29 75 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