简体   繁体   中英

WPF make a streched TextBlock shrink when window is resized

I have a list that shows details of a files. I designed ItemTemplate to stretch DirectoryName TextBox when window is resized.

<ListView ItemsSource="{Binding LogFolderContent}" SelectedItem="{Binding SelectedLogFile}" HorizontalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <TextBlock Text="{Binding LastWriteTime" DockPanel.Dock="Right"/>
                <TextBlock Text="{Binding Name}" DockPanel.Dock="Left"/>
                <TextBlock Text="{Binding DirectoryName}" DockPanel.Dock="Left" Margin="10,0,10,0"/>
            </DockPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This works as expected. 窗宽

Unfortunately when DirectoryName is very long, or window is resized the LastWriteTime disappears from view.

窗户窄

I would very much like to always show LastWriteTime and to do that I would like shrink the TextBlock to not display left part of the DirectoryName in a way that doesn't involve any sidebars.

Is it possible?

This should do it:

<ListView ItemsSource="{Binding LogFolderContent}" SelectedItem="{Binding SelectedLogFile}" HorizontalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Name}" />
                <TextBlock Grid.Column="1" Text="{Binding DirectoryName}"/>
                <TextBlock Grid.Column="2" Text="{Binding LastWriteTime}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Apparently all I had to do is to disable the ScrollViewer that's part of the ListView control, and change HorizontalAlignment to Right for the DirectoryName to Disappear from the left side when window was resized.

<ListView ItemsSource="{Binding LogFolderContent}" SelectedItem="{Binding SelectedLogFile}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <TextBlock Text="{Binding LastWriteTime}" DockPanel.Dock="Right"/>
                <TextBlock Text="{Binding Name}" DockPanel.Dock="Left"/>
                <TextBlock Text="{Binding DirectoryName}" HorizontalAlignment="Right" DockPanel.Dock="Left" Margin="10,0,10,0"/>
            </DockPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

and it looks like this now:

正确的窗口小

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