1

我有一个用户控件,它有一个网格,其规格如下:

<Grid Name="fieldsGrid" Margin="15,0,0,10">
           <Grid.ColumnDefinitions>
               <ColumnDefinition Width="25"/>
               <ColumnDefinition Width="*"/>
               <ColumnDefinition Width="Auto"/>
               <ColumnDefinition Width="Auto"/>
               <ColumnDefinition Width="Auto"/>
           </Grid.ColumnDefinitions>
</Grid>

第二列是感兴趣的列。它显示一个复选框,其文本包含在 TextBlock 中并且看起来"Text..."好像太长了。这是通过代码隐藏(我有我的理由)完成的,如下所示:

CheckBox currentCheckbox = new CheckBox();
TextBlock block = new TextBlock();
block.MaxWidth = 100;
block.Text = text;
block.TextWrapping = TextWrapping.NoWrap;
block.TextTrimming = TextTrimming.WordEllipsis;
currentCheckbox.Content = block;
currentCheckbox.ToolTip = metaText;
currentCheckbox.SetValue(Grid.RowProperty, rowNumber);
currentCheckbox.SetValue(Grid.ColumnProperty, 1);
currentCheckbox.Margin = new Thickness(0, 10, 10, 0);
currentCheckbox.VerticalAlignment = VerticalAlignment.Center;
fieldsGrid.Children.Add(currentCheckbox);

问题是我想在调整 UserControl 大小时扩展此复选框,从而随着大小的增加显示更多文本。如何实现?

4

2 回答 2

1

你为什么不试试这个,从而删除代码隐藏?

<Grid Name="fieldsGrid"
      Margin="15,0,0,10">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="25" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <CheckBox Grid.Column="1"
            Margin="0 10 10 0">
    <CheckBox.Content>
      <!--  Make a binding for the Text of TextBlock below  -->
      <TextBlock MaxWidth="{Binding ActualWidth,
                                    RelativeSource={RelativeSource FindAncestor,
                                                                   AncestorType={x:Type CheckBox}}}"
                 Text="Some Text LOng FFFFFFF DDDDDDDDDDDD"
                 TextTrimming="WordEllipsis"
                 TextWrapping="NoWrap" />
    </CheckBox.Content>
  </CheckBox>
</Grid>

这应该为您提供您正在寻找的行为并完成您的代码隐藏所做的一切(您需要将文本的绑定排序到TextBlockofc)

根据 OP 的特定要求(如果可以,应避免使用代码隐藏):

可以添加你得到的代码隐藏(最后)

currentCheckbox.SizeChanged += (sender, args) => block.MaxWidth = args.NewSize.Width;  // Subtract the checkbox indicator width from args.NewSize.Width here for accurate TextBlock Width measurement

视频链接

于 2013-04-15T20:26:45.477 回答
0

这似乎对我有用。我使用 XAML 只是为了定义 CheckBox 和包含它的标签内容的 TextBlock,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="603.433" Width="730.97">
    <Grid Name="fieldsGrid"
      Margin="15,0,0,10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="Column 1"></TextBlock>
        <TextBlock Grid.Column="2" Text="Column 3, a bit bigger"></TextBlock>
        <TextBlock Grid.Column="3" Text="0" x:Name="txtWidth"></TextBlock>
        <TextBlock Grid.Column="4" Text="Column 5, really really really big"></TextBlock>
        <CheckBox Grid.Column="1" x:Name="testChk"
            Margin="0 10 10 0">
            <CheckBox.Content>
                <TextBlock x:Name="txtForCheckBox"></TextBlock>
            </CheckBox.Content>
        </CheckBox>
    </Grid>
</Window>

然后代码隐藏看起来像这样:

公共主窗口(){

    InitializeComponent();
    testChk.SizeChanged += testChk_SizeChanged;

    txtForCheckBox.Text = "Some text, test, test, test.";
    txtForCheckBox.TextTrimming = TextTrimming.WordEllipsis;
    txtForCheckBox.TextWrapping = TextWrapping.NoWrap;
}

void testChk_SizeChanged(object sender, SizeChangedEventArgs e)
{
    txtForCheckBox.MaxWidth = testChk.ActualWidth;
    txtWidth.Text = testChk.ActualWidth.ToString();
}

希望有帮助...

于 2013-04-15T21:13:51.747 回答