WPF:在CheckBox中TextTrimming没有固定宽度 [英] WPF: TextTrimming in CheckBox with no fixed width

查看:375
本文介绍了WPF:在CheckBox中TextTrimming没有固定宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于网格中的复选框,列跨度为3,其中2列可调整大小,并且可以在运行时调整大小。



我想要当它的文本不适合时显示省略号的复选框,但不能得到这个工作。



这是我的XAML代码。

 <网格> 
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
...
< CheckBox IsEnabled =FalseGrid.Row =2Padding =5Margin =11,12,0,0Name =chkSelectedCatVerticalAlignment =顶部Horizo​​ntalAlignment =StretchGrid.ColumnSpan =3>
< AccessText Text =仅在所选类别中搜索。的TextTrimming = CharacterEllipsis/>
< / CheckBox>

预先感谢您的帮助。

编辑



CheckBox 问题位于 Grid ,它又被包含在一个 GroupBox 中,它位于列的旁边,该列使用 GridSplitter

解决方案

问题在于 GroupBox 将无限宽度分配给其子控件,如 MaxWidth 到 CheckBox ,文本修整工作并显示省略号。



然而, CheckBox 运行期间需要更改的最大宽度。



我试过使用 MultiValueConverter 绑定 CheckBox MaxWidth c>到三列中的总数 ActualWidth ,但 Convert 方法仅在初始化过程中被调用(同样,I认为这是与 GroupBox 为其子控件分配大小的方式有关。)



无论如何,我设法通过删除 ChechkBox AccessText 并使用 TextBlock > StackPanel 代替,然后用 Border 来包围所有元素。

 < Border Grid.Row =2Grid.ColumnSpan =3Name =chkBorderSizeChanged =chkBorder_SizeChanged> 
< StackPanel Orientation =Horizo​​ntalVerticalAlignment =Stretch>
< CheckBox IsEnabled =FalsePadding =0Margin =11,7,0,5Name =chkSelectedCatVerticalAlignment =Center/>
< TextBlock Foreground =BlackName =txtChkSelectedCatText =仅在所选类别中搜索。 TextTrimming =CharacterEllipsisTextWrapping =NoWrapVerticalAlignment =CenterMargin =0,5,5,5/>
< / StackPanel>
< / Border>

改变 Border 的大小时,我更新事件处理程序中的StackPanel的 Width ,并且 TextBox 中的文本相应地修剪。 p>

  private void chkBorder_SizeChanged(object sender,SizeChangedEventArgs e)
{
this.txtChkSelectedCat.Width = this.chkBorder .ActualWidth - this.chkSelectedCat.ActualWidth - 11 - 5 - 5; // margins
}

希望这可以帮助未来的人(即使它是排序的
$ b $ p UPDATE



如果您可以消除 StackPanel 并使用 Grid 来替代, TextTrimming 如下图所示。

 < Grid Grid.Row =2Grid.ColumnSpan =3 > 
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< CheckBox Grid.Column =0IsEnabled =FalsePadding =0Margin =11,7,0,5Name =chkSelectedCatVerticalAlignment =CenterToolTipService.ShowOnDisabled =真/>
< / Grid>

确保将宽度设置为包含文本框到*的列,因为Auto基本上告诉 TextBox 它可以有足够的空间,所以 TextTrimming 不起作用。


I have a checkbox located in a grid with a column span of 3, where 2 of the columns are resizable, and can be resized during runtime.

I want the checkbox to show ellipsis when its text doesn't fit, but can't get this to work.

Here is my XAML code.

<Grid>
     <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
     </Grid.ColumnDefinitions>
...
    <CheckBox IsEnabled="False"  Grid.Row="2" Padding="5" Margin="11,12,0,0" Name="chkSelectedCat" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.ColumnSpan="3">
         <AccessText Text="Search in selected category only." TextTrimming="CharacterEllipsis"/>
    </CheckBox>

Thanks in advance for your help.

EDIT

The CheckBox in question is located within a Grid, which is in turn contained within a GroupBox, which is in a column adjacent to a column which is resized using a GridSplitter.

解决方案

The problem was that the GroupBox allots infinite width to its child controls, as discussed at this MSDN forum thread.

In fact, when setting a MaxWidth to the CheckBox, text trimming worked and the ellipsis were displayed.

However the CheckBox maximum width needed to be changed during runtime.

I tried using a MultiValueConverter to bind the MaxWidth of the CheckBox to the total ActualWidth of the three columns, but the Convert method was only being called during initialisation (again I think this is something to do with how the GroupBox assigns sizes to its child controls).

Anyway I managed to achieve what I wanted by removing the ChechkBox's AccessText and using a TextBlock in a StackPanel in its stead, and then surrounding everything with a Border.

<Border Grid.Row="2" Grid.ColumnSpan="3" Name="chkBorder" SizeChanged="chkBorder_SizeChanged">
     <StackPanel Orientation="Horizontal" VerticalAlignment="Stretch">
               <CheckBox IsEnabled="False" Padding="0" Margin="11,7,0,5" Name="chkSelectedCat"  VerticalAlignment="Center"/>
               <TextBlock Foreground="Black" Name="txtChkSelectedCat" Text="Search in selected category only." TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" VerticalAlignment="Center" Margin="0,5,5,5"/>
      </StackPanel>
</Border>

When the size of the Border is changed, I update the Width of the StackPanel in the event handler, and the text in the TextBox trims accordingly.

private void chkBorder_SizeChanged(object sender, SizeChangedEventArgs e)
{
     this.txtChkSelectedCat.Width = this.chkBorder.ActualWidth - this.chkSelectedCat.ActualWidth - 11 - 5 - 5; // margins
}

Hope this helps someone in the future (even though it is sort of a hack).

UPDATE

If you can eliminate the StackPanel and use a Grid instead, TextTrimming works automatically, without having to do any event handling, as shown below.

<Grid Grid.Row="2" Grid.ColumnSpan="3">
     <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <CheckBox Grid.Column="0" IsEnabled="False" Padding="0" Margin="11,7,0,5" Name="chkSelectedCat" VerticalAlignment="Center" ToolTipService.ShowOnDisabled="True"/>
     <TextBlock Grid.Column="1" Foreground="Black"  Name="txtChkSelectedCat" Text="Search in selected category only." TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" VerticalAlignment="Center" Margin="0,5,5,5" MouseLeftButtonDown="txtChkSelectedCat_MouseLeftButtonDown" ToolTip="{Binding ElementName=chkSelectedCat, Path=ToolTip}" />
</Grid>

Make sure to set the Width of the column containing the textbox to "*", because "Auto" basically tells the TextBox that it can have as much space as it wants, so TextTrimming would not work.

这篇关于WPF:在CheckBox中TextTrimming没有固定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆