简体   繁体   English

WPF中屏幕尺寸减小时如何将标签设置为多行

[英]How to set label to multiple line when screen size decrease in wpf

I am creating desktop application using wpf. 我正在使用wpf创建桌面应用程序。 I have grid in which row is divided in three columns. 我有网格,其中行分为三列。 When the screen size is larger all working well. 当屏幕较大时,一切正常。 but when I decrease screen size. 但是当我减小屏幕尺寸时。 label get cropped. 标签被裁剪。 I have attached both the scenario 我已经附上了两种情况

Larger Screen: http://prntscr.com/8kxuas Small Screen: http://prntscr.com/8kxufe 大萤幕: http//prntscr.com/8kxuas小萤幕: http//prntscr.com/8kxufe

Here is the code for grid: 这是网格的代码:

<Window x:Class="CharunOptics.Search"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Charun Optics" Height="800px" Width="1600px" WindowStartupLocation="CenterScreen" WindowState="Maximized" Loaded="Window_Loaded">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="7*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="3*"></ColumnDefinition>
            <ColumnDefinition Width="1*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Label Content="Search: (Name or Contact)" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Margin="0,0,25,0" VerticalAlignment="Center" FontSize="20"/>
            <TextBox Grid.Column="1" HorizontalAlignment="Stretch" Height="auto" Margin="25,0,25,0" TextWrapping="Wrap" Text="" VerticalAlignment="Center" FontSize="20"  Grid.Row="0" Name="TxtName"/>
            <Button x:Name="BtnSearch"  Grid.Column="2" Grid.Row="0" Content="Search" FontSize="20" Click="BtnSearch_Click" Margin="32"/>
        </Grid>

    </Grid>
    <DataGrid Grid.Row="1" Grid.Column="0" Name="DtGrid" ColumnWidth="*" CanUserAddRows="False">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
                <Setter Property="Foreground" Value="{StaticResource PrimaryFont}" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

</Grid>

Is it possible to set label in multiple line when it is overflow just like HTML? 是否可以像HTML一样在溢出时在多行中设置标签?

I find the solution for it. 我找到了解决方案。 I've change the Lable to TextBlock, as lable don't have textwrapping property. 我将标签更改为TextBlock,因为标签没有textwrapping属性。

Thanks to @Frisbee for your advise 感谢@Frisbee为您提供建议

<TextBlock Text="Search: (Name or Contact)" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" Margin="0,0,25,0" VerticalAlignment="Center" FontSize="20" TextWrapping="Wrap"/>

first: 第一:

wpf Label dose not support text wrapping. wpf标签不支持文本换行。 a better idea is to use text wrapping 一个更好的主意是使用文字换行

a better idea is to use TextBlock which supports text wrapping. 更好的主意是使用支持文本换行的TextBlock。

<TextBlock TextWrapping="Wrap">Your huge text here</TextBlock>

You just set the width/ height- property to Auto (it's the default value). 您只需将width / height-属性设置为Auto (这是默认值)。 That will set the size of the label base of the screen size. 这将设置屏幕大小的标签基大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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