2010-08-02 58 views
0

簡化的問題。對齊控件的尺寸

我有一個位於一個在另一個也必將給同一序列的兩個ItemsControl S:

[Control] [Another Control] [More Controls] 
[Other Ctrl] [Super Control] [Control] 

我想這樣做的是調整其大小,使得它們看起來是單個表:

[Control ] [Another Control] [More Controls] 
[Other Ctrl] [Super Control ] [Control  ] 

這也是可以接受去「單向」:使底部項目大小相同頂級的,即使是短:

[Control] [Another Control] [More Controls] 
[Other C] [Super Control ] [Control  ] 

幾個方面的考慮:

  1. 是的,他們是在單獨的ItemControl秒。
  2. 不,我不能使用GridDataGrid
  3. 我完全擁有後端數據結構(是否有幫助)

更完整的解釋,一個可以參考我other question

UPDATE:爲什麼他們要在不同的ItemControls
一般情況下,我想要實現的是建立一個綁定到一個矩形的數據結構的表。除了使用嵌套的ItemControl s:一個ItemControl作爲行,每個項目包含另一個ItemControl用於該行中的單元格,我沒有看到任何其他方式來執行此操作。反之亦然:一個用於列,每個項目包含該列中的單元格。無論哪種方式,嵌套的項目將不得不彼此對齊。
我知道可能有其他方法來創建這樣的表格,但我沒有看到。這也是我爲什麼要求other question的原因。

謝謝,哦,全能的社區,提前。

+0

如果你能解釋*爲什麼你不能使用網格,這將有所幫助。有沒有其他佈局,你不能使用?爲什麼不? – 2010-08-02 06:55:12

+0

在我的其他問題中有更完整和一般化的解釋:http://stackoverflow.com/questions/3385344/grid-with-decoupled-scrolling。特別是,不能使用Grid,因爲它無法自動生成數據序列中的行/列。我也更新了一下這個問題,看看是否有幫助。 – 2010-08-02 15:51:08

回答

0

嘗試以下操作:

<StackPanel Grid.IsSharedSizeScope="True"> 
    <StackPanel.Resources> 
     <DataTemplate x:Key="SharedSizeTemplate"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" SharedSizeGroup="SharedSize"/> 
       </Grid.ColumnDefinitions> 
       <TextBlock Text="{Binding}"/> 
      </Grid> 
     </DataTemplate> 
    </StackPanel.Resources> 
    <ItemsControl ItemTemplate="{StaticResource SharedSizeTemplate}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <System:String>One</System:String> 
     <System:String>Longer</System:String> 
     <System:String>Longest</System:String> 
     <System:String>Very Longest</System:String> 
    </ItemsControl> 
    <ItemsControl ItemTemplate="{StaticResource SharedSizeTemplate}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <System:String>Short</System:String> 
     <System:String>S</System:String> 
     <System:String>Even longer than above</System:String> 
     <System:String>V</System:String> 
    </ItemsControl> 
</StackPanel> 

編輯:還有一個問題,但 - 所有列將大小相同的最佳寬度 - 不知道,如果你能忍受的?

+0

哇,非常感謝!老實說,我已經開始認爲我必須手動創建/管理UI元素。所有列的寬度都相同:我實際上可以爲每列創建單獨的字符串ID,將它們放入我的數據結構中,並將適當列的'SharedSizeGroup'屬性綁定到'ColumnID'屬性,從而使它們全都不同尺寸。 – 2010-08-02 20:57:39

+0

但是,這會產生另一個問題:只要我在'ColumnDefinition'上放置'SharedSizeGroup'屬性,該列將停止伸展以填充可用空間。你知道,我需要把最後一欄延伸到最後。任何想法如何獲得? – 2010-08-02 20:58:42

+0

嗯...那麼你需要搗亂一個DataTemplateSelector - 基於項目在兩個DataTemplates之間的列表開關中的位置......這可能會變得混亂......或者爲ItemsPanelTemplate實現一個自定義面板 - 讓我知道你更喜歡哪一個,我會更新一個樣本。 – Goblin 2010-08-03 06:03:52

0

最近我有類似的問題,最後我用DockPanel中這樣做是用我自己的簡單的黑客針對的DataTemplate:

<DockPanel VerticalAlignment="Center" HorizontalAlignment="Stretch" LastChildFill="True"> 
    <TextBox DockPanel.Dock="Right" Width="40" Text="{Binding CurrentValue}"></TextBox> 
    <Slider DockPanel.Dock="Right" Width="60" ...></Slider> 
    <TextBlock DockPanel.Dock="Right" Text="{Binding Header}"></TextBlock> 
</DockPanel> 

你可以使用這個主意,如果你可以決定幾乎大多數控件寬度,或者根據需要製作嵌套滾動視圖。