0

我正在尝试在运行时创建带有自动换行的复选框。带有自动换行的复选框的 XAML 如下所示:

<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

谢谢,彼得

4

1 回答 1

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)
于 2013-01-10T14:11:28.253 回答