2011-11-10 12 views
2

問題:いくつかの項目を持つItemsControlを持つウィンドウ(四角形と言う) ウィンドウにMinWidthとMinHeightが設定されています(例:300) 長方形に2つの列に表示するのに十分なスペースがない場合、ウィンドウのサイズを変更するときに必要です。 そして、2列の中にまだスクロールビューアを表示するのに十分なスペースがない場合。 1.拡張のItemsControlを作成します:WPF - IScrollInfoの実装

パブリッククラスMyGrid:グリッド、IScrollInfo

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <local:MyGrid IsItemsHost="True" x:Name="PART_ItemsPanel" Initialized="OnItemsPanelInitialized" CanVerticallyScroll="True" CanHorizontallyScroll="True"> 
      <local:MyGrid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
      </local:MyGrid.ColumnDefinitions> 
      <local:MyGrid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
      </local:MyGrid.RowDefinitions> 
     </local:MyGrid> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 

私のItemsControlはItemsPanelTemplate拡張グリッドとして持っている私が試した何

{ .... IScrollInfoの実装 }

ItemsControlがPrepareContainerForItemOverride()を呼び出すときに、これを使用してアイテムを2つの列に分割することができると考えるこのグリッドを使用します。

ideeaは、会議から「取らない」された....しかし、私は私のような質問が...次に何をすべきか を知らない:私は測定をオーバーライドして、データグリッドの手配、私はの位置を設定する際 をDataGridの項目が、それからPrepareContainerForItemOverride()と呼ばれます。私は行数を計算しなければなりません。しかし、私は再びウィンドウのサイズを変更する場合は... PrepareContainerForItemOverride()は呼び出されません...

これは私の上にこの問題は...あなたが誰かがある場合は私に手がかりを与えてください。 ありがとう!

答えて

0

私は私が撮った...誰がそれを必要とする場合解決策を共有したいと思いました。

IScrollInfoの実装を避けるため、ListBoxコントロールでItemsControlを変更しました。だから私はアレンジよりも優先されます。 アレンジでは、最初の列のアイテムの高さを計算し、アイテムをGridPanelに配置します。私はscrollViewer配列が常に有効なわけではないので、ディスパッチャを使用して、私は可視高さを得るためにそれを数えています。

<ListBox x:Uid="allListBox" x:Name="adminListBoxControl" ItemTemplate ="{DynamicResource LinkButtonItemTemplate}" Margin="15" BorderBrush="Transparent" Background="Transparent"> 
      <ListBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <local:GridPanel> 
         <local:GridPanel.ColumnDefinitions> 
          <ColumnDefinition Width="0.45*"/> 
          <ColumnDefinition Width="0.45*"/> 
          <ColumnDefinition Width="0.1*"/> 
         </local:GridPanel.ColumnDefinitions> 
         <local:GridPanel.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="Auto"/> 
         </local:GridPanel.RowDefinitions> 
        </local:GridPanel> 
       </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
     </ListBox> 

protected override Size ArrangeOverride(Size finalSize) 
    { 
     Size arrangeSize = base.ArrangeOverride(finalSize); 

     lastItemInFirstColumn = -1; 
     double totalHeight = 0; 

     DispatcherPriority p = ScrollViewer.IsArrangeValid ? DispatcherPriority.Normal : DispatcherPriority.Input; 
     if (o != null) 
      o.Abort(); 

     o = Dispatcher.BeginInvoke(p, (Action)(() => 
     { 
      double fitHeight = ScrollViewer.ActualHeight; 
      foreach (UIElement child in this.InternalChildren) 
      { 
       if (totalHeight + child.DesiredSize.Height < fitHeight) 
       { 
        totalHeight += child.DesiredSize.Height; 
        lastItemInFirstColumn++; 
       } 
       else 
       { 
        if (lastItemInFirstColumn + 1 < InternalChildren.Count - (lastItemInFirstColumn + 1)) 
         lastItemInFirstColumn++; 
        break; 
       } 
      } 

      //set items positions 
      ArrangeItemsInGrid(); 
     })); 

     o.Completed += (s, e) => { o = null; }; 
     return arrangeSize; 
    } 
+1

これがあなたのために働く解決策であれば、この質問に対する答えとして答えを受け入れてください。 – Nightfirecat

0

あなたの必要とするすべてのものは、あなたのItemsControlの別のパネルです。それはあなたがビルトインWrapPanel使用して逃げるだろうとかもしれないが、あなたはまた、あなた自身を書くことができ:

<ItemsControl ...> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <!-- put whatever panel that encapsulates your layout logic here, possibly your own --> 
      <WrapPanel/> 
     </ItemsPanelTemplate> 
    <ItemsControl.ItemsPanel> 
</ItemsControl> 
+0

ありがとうございました!私はラップパネルを試してみましたが、コンテンツを2つの列に入れることができるようにする必要があります(まだビューポートに収まらない場合はスクロールできます)。私は、ISCrollViewerを実装し、オーバーライドされた配列、測度を使用する拡張グリッドを使用しました...そして、それは動作するようです。私は(... IF ...)私はソリューションを完全に実装するときにこのポストに戻るでしょう。 – Victor

関連する問題