简体   繁体   中英

WPF TextBox not trimming in DataTemplate

I have an odd problem with TextBox with TextTrimming set to CharacterElipsis used in a DataTemplate in WPF application. At the start of application everything works fine. But when I am resizing the window - reducing the width, the trimming is not working.

In an example below:

<Grid>
        <DockPanel>
            <DockPanel.Resources>
                <DataTemplate x:Key="lowerLevel" >
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="34*" />
                            <ColumnDefinition Width="26*" />
                            <ColumnDefinition Width="26*" />
                            <ColumnDefinition Width="14*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="textboxvalue1" Grid.Column="0" FontWeight="Bold" VerticalAlignment="Center" Margin="15,10,0,10" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" />
                        <TextBlock Text="textboxvalue2" Grid.Column="1" FontWeight="Bold" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap"   />
                        <TextBlock Text="textboxvalue3" Grid.Column="2" FontWeight="Bold" VerticalAlignment="Center"  Margin="0,10,0,10" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap"  />
                        <CheckBox IsChecked="True" Content="ApprovedText"  FontWeight="Bold" Grid.Column="3"  VerticalAlignment="Center"  Margin="0,10,15,10" />
                    </Grid>
                </DataTemplate>
            </DockPanel.Resources>
            <ListView x:Name="listViewControl" ItemTemplate="{StaticResource lowerLevel}" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"  HorizontalContentAlignment="Stretch" />
            
        </DockPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="34*" />
                <ColumnDefinition Width="26*" />
                <ColumnDefinition Width="26*" />
                <ColumnDefinition Width="14*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="textboxvalue1" Grid.Column="0" FontWeight="Bold" VerticalAlignment="Center" Margin="15,10,0,10" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" />
            <TextBlock Text="textboxvalue2" Grid.Column="1" FontWeight="Bold" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap"   />
            <TextBlock Text="textboxvalue3" Grid.Column="2" FontWeight="Bold" VerticalAlignment="Center"  Margin="0,10,0,10" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap"  />
            <CheckBox IsChecked="True" Content="ApprovedText"  FontWeight="Bold" Grid.Column="3"  VerticalAlignment="Center"  Margin="0,10,15,10" />
        </Grid>
    </Grid>

I am having two identical Grids, but first one is placed in a DataTemplate and the second is just a separate control. TextBoxes in bottom grid are trimming correctly, when resizing window, however the TextBoxes from DataTemplate do not fit the width of parent columns while window resizing.

The code behind is:

public partial class MainWindow : Window
    {
        private ListCollectionView comparedFamiliesView;
        public ListCollectionView ComparedFamiliesView
        {
            get
            {
                if (comparedFamiliesView == null)
                {
                    comparedFamiliesView = new ListCollectionView(new List<Object>() { new Object(), new Object(), new Object() });
                }
                return comparedFamiliesView;
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            listViewControl.ItemsSource = ComparedFamiliesView;
        }
    }

It basically adds three objects to have something to view on ListView.

I was trying different combinations of VerticalAlignment, VerticalContentAlignment - didn't work. What I have tried was to place each TextBox in separate Grid in order to bind its Width to TextBox Width or MaxWidth, like:

<Grid Grid.Column="0" x:Name="grid1">
    <TextBlock Text="textboxvalue1" FontWeight="Bold" VerticalAlignment="Center" Margin="15,10,0,10" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" MaxWidth="{Binding ActualWidth, ElementName=grid1}" />
</Grid>

Didn't work either.

How can I force TextBoxes in DataTemplate to behave the same way as those in separate Grid? Or what am I doing wrong with TextTrimming when using in DataTemplate.

Thank you for your help!!!

Regards,

Ariel

Try to set the ScrollViewer.HorizontalScrollBarVisibility attached property of the ListView to Disabled :

<ListView x:Name="listViewControl" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM