2016-05-03 24 views
0

我想能夠將大按鈕添加到我的ItemsControl。巨人按鈕應該包含在裏面多的TextBlocks畫布,像下面的XAML代碼的按鈕:在c中添加畫布和TextBlocks到巨大的按鈕#

<DockPanel> 
    <Button DockPanel.Dock="Right" Click="add_Click">Add</Button> 

    <ItemsControl Name="itemsControl"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Vertical" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <Button Width="450" Height="150" Margin="5" Name="button1" Background="DarkGreen"> 
      <Canvas> 
       <TextBlock Canvas.Right="80" Canvas.Bottom="30" Background="RoyalBlue" Width="60" TextAlignment="Center" Name="person1">OR1</TextBlock> 
       <TextBlock Canvas.Right="10" Canvas.Bottom="30" Background="SkyBlue" Width="60" TextAlignment="Center" Name="person2">Smith</TextBlock> 
       <TextBlock Canvas.Left="40" Canvas.Bottom="30" Background="Goldenrod" Width="60" TextAlignment="Center" Name="person3">CAM1</TextBlock> 
       <TextBlock Canvas.Left="110" Canvas.Bottom="30" Background="HotPink" Width="60" TextAlignment="Center" Name="person4">Doe</TextBlock> 
      </Canvas> 
     </Button> 
    </ItemsControl> 
</DockPanel> 

如何我在XAML在我的C#add_Click功能添加一個巨大的按鈕,像一個?我嘗試過在線搜索,但一直沒能找到任何可以實現類似的功能。

回答

0

根據你想要做什麼,你有幾個選擇。如果您只想「添加」少量按鈕(2-3),則可以更改add_Click方法中每個按鈕的可見性。

button1.Visibility = Visibility.Collapsed; 

這不是最好的選擇,但它在技術上很容易。

更好的方法是build a user control包含你想要的一切。然後在視圖模型中創建這些按鈕的可觀察集合。一旦完成,你可以bind the item control itemsource to your collection

最後,在您的代碼隱藏中,您可以創建這些按鈕的集合,例如

GiantButtonCollection.Add(new GiantButton()) 

這是更好的(但需要更多的工作/知識)