2010-05-12 53 views
9

我有一個Accordion,其內容的高度可以動態調整大小。我希望手風琴能夠動態響應兒童項目的高度,但我很難做到這一點。動態調整打開的手風琴的大小

<lt:Accordion Name="MyAccordion" 
        SelectionMode="ZeroOrOne" 
        HorizontalAlignment="Stretch"> 
     <lt:AccordionItem Name="MyAccordionItem" 
          Header="MyAccordion" 
          IsSelected="True" 
          HorizontalContentAlignment="Stretch" 
          VerticalAlignment="Stretch"> 
      <StackPanel> 
       <Button Content="Grow" Click="Grow"/> 
       <Button Content="Shrink" Click="Shrink"/> 
       <TextBox Name="GrowTextBox" 
         Text="GrowTextBox" 
         Height="400" 
         Background="Green" 
         SizeChanged="GrowTextBox_SizeChanged"/> 
      </StackPanel> 
     </lt:AccordionItem> 
    </lt:Accordion> 


    private void Grow(object sender, System.Windows.RoutedEventArgs e) 
    { 
     GrowTextBox.Height += 100; 
    } 

    private void Shrink(object sender, System.Windows.RoutedEventArgs e) 
    { 
     GrowTextBox.Height -= 100; 
    } 

    private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e) 
    { 
     MyAccordion.UpdateLayout(); 
     MyAccordionItem.UpdateLayout(); 
    } 

你要知道,如果我崩潰,然後重新打開手風琴,它需要的形狀只是我想要的方式,但我想,當孩子調整大小以立即發生這種大小調整。

我無力地嘗試通過添加一個調用Accordion和AccordionItem的UpdateLayout()的SizeChanged事件處理程序來解決此問題,但這沒有任何視覺效果。我無法弄清楚在Accordion控件中適當調整大小的位置。有人有想法嗎?

回答

1

試試這個

//here i am creating a size object depending on child items height and width 
     // and 25 for accordian item header... 
     // if it works you can easily update the following code to avoid exceptional behaviour 
     Size size = new Size(); 
     size.Width = GrowTextBox.ActualWidth; 
     size.Height = grow.ActualHeight + shrink.ActualHeight + GrowTextBox.ActualHeight + 25; 
     MyAccordion.Arrange(new Rect(size)); 

在上面的代碼中,我只是根據子項大小重新安排手風琴。

1

我有類似的問題,我簡單的黑客如下:

private void GrowTextBox_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e) 
{ 
     MyAccordionItem.Measure(new Size()); 
     MyAccordionItem.UpdateLayout(); 
} 

希望它爲你工作太..

乾杯

+0

嗯,這聽起來很有希望,但它似乎並沒有解決問題。我可以看到你的XAML是什麼樣的嗎? – 2010-05-14 23:04:22

+0

我有一個datagrid內的數據網格,並在內部網格的sizechanged,我包括代碼如上。讓我試試按鈕,然後回到你身邊。 – Joshscorp 2010-05-18 00:49:29

1

我有一個稍微不同的問題 - 調整窗口大小有時並未正確調整Accordion項目大小,因此下一個項目的標題將卡在窗口下方或中間。

我通過創建一個在SizeChanged中啓動的計時器來解決此問題,並且取消選擇並立即重新選擇當前項目,之後佈局似乎被重新調整並變爲正確。也可以幫助你。你可以省去定時器,當用戶拖動調整窗口大小時,我會介紹它以防止連續呼叫,但由於延遲,它也會產生一種羽毛效果。

public partial class MyAccordion : System.Windows.Controls.Accordion 
{ 
    private Timer _layoutUpdateTimer = new Timer(100); 

    public MyAccordion 
    { 
     this.SizeChanged += (s, e) => 
     { 
      _layoutUpdateTimer.Stop(); // prevents continuous calls 
      _layoutUpdateTimer.Start(); 
     }; 
     _layoutUpdateTimer.Elapsed += (s, e) => ReselectItem(); 
    } 

    private void ReselectItem() 
    { 
     Application.Current.Dispatcher.BeginInvoke((Action)(() => 
     { 
      // backup values 
      int selectedIndex = this.SelectedIndex; 
      AccordionSelectionMode mode = this.SelectionMode; 

      // deselect 
      this.SelectionMode = AccordionSelectionMode.ZeroOrOne; // allow null selection 
      this.SelectedItem = null; 

      // restore values (reselect) 
      this.SelectionMode = mode; 
      this.SelectedIndex = selectedIndex; 
     })); 
     _layoutUpdateTimer.Stop(); 
    } 
}