2013-01-07 105 views
0

我試圖在運行時創建帶有自動換行的複選框。與自動換行復選框的XAML看起來是這樣的:在運行時創建TextBlock

<CheckBox Width="140" Height="35"> 
    <ContentControl> 
     <TextBlock TextWrapping="Wrap">This is a long text with word wrap</TextBlock> 
    </ContentControl> 
</CheckBox> 

現在我要創建這個用XAML代碼,但我不知道如何得到它的工作。我能夠創建複選框並將其添加到現有的WrapPanel,但textBlock控件沒有內容屬性。我如何將內容添加到textBlock,以及如何將兩者(contentControl和textBlock)添加到複選框?

For intIndex = 0 To m_aryActions.Length - 1 

    Dim textBlock As TextBlock = New TextBlock 
    Dim contentControl As ContentControl = New ContentControl 
    Dim checkBox As CheckBox = New CheckBox 

    textBlock.TextWrapping = TextWrapping.Wrap 
    contentControl.Content = textBlock 

    With checkBox 
     .Width = 140 
     .Height = 25 
     .Name = "CheckBox" & intIndex 
    End With 

    WrapPanel.Children.Add(checkBox) 

Next 

謝謝, 彼得

+0

沒人的想法?太容易或經常被問到?我已經搜索,但沒有找到解決方案。太難了?我不相信。 – user1954535

回答

0

好吧,我自己覺得:

Dim textBlock As TextBlock = New TextBlock 
Dim contentControl As ContentControl = New ContentControl 
Dim checkBox As CheckBox = New CheckBox 

With textBlock 
    .TextWrapping = TextWrapping.Wrap 
    .Text = m_aryWishes(intIndex) 
End With 

contentControl.Content = textBlock 

With checkBox 
    .Width = intWidth 
    .Height = intHeight 
    .Content = contentControl 
    .Name = "CheckBox" & intIndex 
    .Padding = New System.Windows.Thickness(10, 0, 20, 0) 
    .Focusable = False 
    .ClickMode = ClickMode.Press 
End With 

WrapPanel.Children.Add(checkBox)