Results 1 to 4 of 4

Thread: [WPF] TextBlock in Border shrink below text size

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    [WPF] TextBlock in Border shrink below text size

    Hi,

    I am building a 'DataGrid' from scratch. The main element is a Grid with 4 columns and 3 rows. These 4 columns also represent the 4 columns of the 'DataGrid' (there will always be 4 columns, and the user cannot resize them. This is not a generic grid, it only fits this one project with the data I have). The 3 rows contain:
    1. A row of 4 column headers (Borders with TextBlocks inside)
    2. A row of 4 ComboBoxes (to filter the data rows)
    3. A ListBox (spans all 4 columns) to show the data rows.

    The ListBox has an ItemTemplate with another Grid with the same 4 columns (so that the headers, comboboxes and data columns are the same width), each of which contains a Border with a TextBlock to hold a specific part of the data.

    The XAML is this:
    xml Code:
    1. <UserControl x:Class="PaperManager.Controls.PapersGrid"
    2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6.              mc:Ignorable="d"
    7.              d:DesignHeight="422" d:DesignWidth="642">
    8.    
    9.     <UserControl.Resources>
    10.         <Style TargetType="ComboBox">
    11.             <Setter Property="Margin" Value="2" />
    12.         </Style>
    13.  
    14.         <Style x:Key="StretchedContainerStyle" TargetType="{x:Type ListBoxItem}">
    15.             <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    16.         </Style>
    17.  
    18.         <Style x:Key="ItemBorderStyle" TargetType="Border">
    19.             <Setter Property="BorderBrush" Value="LightGray" />
    20.             <Setter Property="BorderThickness" Value="1" />
    21.             <Setter Property="CornerRadius" Value="1" />
    22.             <Setter Property="Margin" Value="1" />
    23.             <Setter Property="Padding" Value="2" />
    24.         </Style>
    25.  
    26.         <Style x:Key="HeaderBorderStyle" TargetType="Border" BasedOn="{StaticResource ItemBorderStyle}">
    27.             <Setter Property="BorderBrush" Value="#FFFFFF" />
    28.                 <Setter Property="BorderThickness" Value="0" />
    29.             <Setter Property="CornerRadius" Value="0" />
    30.             <Setter Property="Margin" Value="-1" />
    31.             <Setter Property="Padding" Value="2" />
    32.             <Setter Property="Height" Value="35" />
    33.             <Setter Property="Background">
    34.                 <Setter.Value>
    35.                     <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
    36.                         <GradientStop Offset="0" Color="AliceBlue" />
    37.                         <GradientStop Offset="0.3" Color="LightBlue" />
    38.                         <GradientStop Offset="0.3" Color="LightBlue" />
    39.                         <GradientStop Offset="1.3" Color="AliceBlue" />
    40.                     </LinearGradientBrush>
    41.                 </Setter.Value>
    42.             </Setter>
    43.         </Style>
    44.     </UserControl.Resources>
    45.  
    46.     <Grid>
    47.         <Grid.ColumnDefinitions>
    48.             <ColumnDefinition Width="*" />
    49.             <ColumnDefinition Width="2.5*" />
    50.             <ColumnDefinition Width="*" />
    51.             <ColumnDefinition Width="*" />
    52.         </Grid.ColumnDefinitions>
    53.         <Grid.RowDefinitions>
    54.             <RowDefinition Height="Auto" />
    55.             <RowDefinition Height="Auto" />
    56.             <RowDefinition Height="*" />
    57.         </Grid.RowDefinitions>
    58.  
    59.         <!-- Headers -->
    60.         <Border Style="{StaticResource HeaderBorderStyle}" Grid.Column="0">
    61.             <TextBlock Text="Authors" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    62.         </Border>
    63.         <Border Style="{StaticResource HeaderBorderStyle}" Grid.Column="1">
    64.             <TextBlock Text="Title" HorizontalAlignment="Center" VerticalAlignment="Center" />
    65.         </Border>
    66.         <Border Style="{StaticResource HeaderBorderStyle}" Grid.Column="2">
    67.             <TextBlock Text="Journal" HorizontalAlignment="Center" VerticalAlignment="Center" />
    68.         </Border>
    69.         <Border Style="{StaticResource HeaderBorderStyle}" Grid.Column="3">
    70.             <TextBlock Text="Categories" HorizontalAlignment="Center" VerticalAlignment="Center" />
    71.         </Border>
    72.  
    73.         <!-- Filter ComboBoxes -->
    74.         <ComboBox Grid.Row="1" Grid.Column="0" IsEditable="True" Text="{Binding Path=SelectedAuthor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Authors, UpdateSourceTrigger=PropertyChanged}" IsTextSearchEnabled="False" />
    75.         <ComboBox Grid.Row="1" Grid.Column="1" IsEditable="True" Text="{Binding Path=SelectedTitle, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Titles, UpdateSourceTrigger=PropertyChanged}" IsTextSearchEnabled="False" />
    76.         <ComboBox Grid.Row="1" Grid.Column="2" IsEditable="True" Text="{Binding Path=SelectedJournal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Journals, UpdateSourceTrigger=PropertyChanged}" IsTextSearchEnabled="False" />
    77.         <ComboBox Grid.Row="1" Grid.Column="3" IsEditable="True" Text="{Binding Path=SelectedCategory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=Categories, UpdateSourceTrigger=PropertyChanged}" IsTextSearchEnabled="False" />
    78.  
    79.         <!-- Grid -->
    80.         <ListBox Grid.Row="2" Grid.ColumnSpan="4"
    81.             ItemsSource="{Binding Path=Papers, UpdateSourceTrigger=PropertyChanged}"
    82.             SelectedItem="{Binding Path=SelectedPaper, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    83.             ItemContainerStyle="{StaticResource StretchedContainerStyle}">
    84.             <ListBox.ItemTemplate>
    85.                 <DataTemplate>
    86.                     <Grid >
    87.                         <Grid.ColumnDefinitions>
    88.                             <ColumnDefinition Width="*" />
    89.                             <ColumnDefinition Width="2.5*" />
    90.                             <ColumnDefinition Width="*"  />
    91.                             <ColumnDefinition Width="*"  />
    92.                         </Grid.ColumnDefinitions>
    93.  
    94.                         <Border Grid.Column="0" Style="{StaticResource ItemBorderStyle}">
    95.                             <TextBlock Text="{Binding AllAuthors}" VerticalAlignment="Top" />
    96.                         </Border>
    97.  
    98.                         <Border Grid.Column="1" Style="{StaticResource ItemBorderStyle}">
    99.                             <TextBlock Text="{Binding Title}" VerticalAlignment="Top" FontWeight="Bold" />
    100.                         </Border>
    101.  
    102.                         <Border Grid.Column="2" Style="{StaticResource ItemBorderStyle}">
    103.                             <TextBlock Text="{Binding JournalDetails}" VerticalAlignment="Top" />
    104.                         </Border>
    105.  
    106.                         <Border Grid.Column="3" Style="{StaticResource ItemBorderStyle}">
    107.                             <TextBlock Text="{Binding AllCategories}" VerticalAlignment="Top" />
    108.                         </Border>
    109.                     </Grid>
    110.                 </DataTemplate>
    111.             </ListBox.ItemTemplate>
    112.         </ListBox>
    113.     </Grid>
    114. </UserControl>

    When large enough to accomodate all data, the grid looks like this:



    Now, when I make the window smaller, the columns resize accordingly (since they use "*" widths), which is correct. However, once a columns gets smaller than the width of the text it is displaying in its data row, then it seems the data row no longer cares about the star resizing and simply retains a minimum width in order to keep displaying all the text:


    This is not what I want!


    For now, what I would like to see is that the borders/textblocks in the data rows simply become smaller (the same width as the column headers and comboboxes above) and simply cut off the text.

    A slightly better approach would be to get text trimming to work (so that the text is not just cut off but shows some ellipsis, "..." instead).

    The best approach would be to wrap the text over multiple lines (usually there will be multiple authors / categories so that the data row is already larger than one line, so that is room wasted at the moment).

    I have tried every kind of combination I could think of to get the TextBlocks to wrap (TextWrapping) or trim (TextTrimming), but nothing, the behavior remains as in the second screenshot...


    How can I do this? I don't understand why this is happening in the first place. I clearly tell the columns to have widths *, 2.5*, * and *. This is the case in the first row (the second column is 2.5 times as wide as the three others), but certainly not in the second row (otherwise the columns would be equally long...). So for some reason the textblocks are overriding these width settings?

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width