动态调整WPF文本块大小时性能不佳
我目前正在设计一个WPF应用程序的布局,在我的一个控件的布局中似乎遇到了一些障碍。此控件是一个动态调整大小的控件,因此它应该适合它所属的视口的大小。我遇到的问题是一个非常直观的问题,所以我会尽力描述它。以下是目前的情况: 每个“Col N Row X”标题下的区域是一个文本块,可以在其中放置不同长度的文本。为了使TextBlock实际换行,我在stackoverflow上找到了一个解决方案,即将TextBlock的宽度绑定到列的宽度。以下是网格定义的片段以及第一列的定义:动态调整WPF文本块大小时性能不佳,wpf,performance,grid,textblock,Wpf,Performance,Grid,Textblock,我目前正在设计一个WPF应用程序的布局,在我的一个控件的布局中似乎遇到了一些障碍。此控件是一个动态调整大小的控件,因此它应该适合它所属的视口的大小。我遇到的问题是一个非常直观的问题,所以我会尽力描述它。以下是目前的情况: 每个“Col N Row X”标题下的区域是一个文本块,可以在其中放置不同长度的文本。为了使TextBlock实际换行,我在stackoverflow上找到了一个解决方案,即将TextBlock的宽度绑定到列的宽度。以下是网格定义的片段以及第一列的定义: <!-- Ch
<!-- Change Detail Contents Grid -->
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="270" Width="2*" />
<ColumnDefinition MinWidth="160" Width="*" />
<ColumnDefinition MinWidth="160" Width="*" />
<ColumnDefinition MinWidth="160" Width="*" />
</Grid.ColumnDefinitions>
<!--
We bind the width of the textblock to the width of this border to make sure things resize correctly.
It's important that the margin be set to 1 larger than the margin of the textblock or else you'll end
up in an infinate loop
-->
<Border Grid.Column="0" Margin="6" Name="FirstBorder" />
<Border Grid.Column="0" BorderThickness="0,0,1,0" BorderBrush="{DynamicResource ColumnBorderBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Border Style="{DynamicResource DetailHeadingBorder}">
<TextBlock Text="Col 1 Row 1" Style="{DynamicResource DetailHeadingText}" />
</Border>
<TextBlock Text="{Binding IsReason, ElementName=ChangeDetailRoot}" Style="{DynamicResource DetailText}" Width="{Binding ActualWidth, ElementName=FirstBorder}" />
</StackPanel>
<StackPanel Grid.Row="1">
<Border Style="{DynamicResource DetailHeadingBorder}">
<TextBlock Text="Col 1 Row 2" Style="{DynamicResource DetailHeadingText}" />
</Border>
<TextBlock Text="{Binding WasReason, ElementName=ChangeDetailRoot}" Style="{DynamicResource DetailText}" Width="{Binding ActualWidth, ElementName=FirstBorder}" />
</StackPanel>
</Grid>
</Border>
</Grid>
在每个文本块上。当屏幕上同时显示的控件越多,问题也会变得更严重。但是,如果没有这一行,每个文本块中的文本将继续向右增长,添加的文本越多,而不是在列中向下换行
有没有更好的方法来完成我想要完成的事情?使用HTML/CSS,这将是一件相当简单的事情。我花了几个小时在谷歌上搜索,并在stackoverflow中寻找这个问题的答案
我来自HTML/CSS的深厚背景,所以如果这不是WPF应该擅长的,请告诉我。如果您尝试过,我很抱歉,但是将TextBlock.TextWrapping设置为“Wrap”是否不能实现您的目标 我猜这将消除您正在进行的绑定到宽度的工作,因为网格将负责调整大小。(这可能就是现在发生的情况:网格在收缩时正在布置控件,而对宽度的绑定稍微改变了大小,导致了舞蹈。) [更新] 我试图复制你看到的行为,但对我来说效果很好。我为文本块制作了一个简单的样式,如下所示:
<Style x:Key="DetailText" TargetType="{x:Type TextBlock}">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
我没有任何其他动态资源(DetailHeadingBorder、DetailHeadingText或ColumnBorderBrush
),所以一切都是黑白的(很好)
也许你只是有一个非常旧的图形卡,它在软件中渲染东西?或者这与你的
风格有关
如果你尝试过,我很抱歉,但是将TextBlock.TextWrapping设置为“Wrap”是否不能实现你的目标
我猜这将消除您正在进行的绑定到宽度的工作,因为网格将负责调整大小。(这可能就是现在发生的情况:网格在收缩时正在布置控件,而对宽度的绑定稍微改变了大小,导致了舞蹈。)
[更新]
我试图复制你看到的行为,但对我来说效果很好。我为文本块制作了一个简单的样式,如下所示:
<Style x:Key="DetailText" TargetType="{x:Type TextBlock}">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
我没有任何其他动态资源(DetailHeadingBorder、DetailHeadingText或ColumnBorderBrush
),所以一切都是黑白的(很好)
也许你只是有一个非常旧的图形卡,它在软件中渲染东西?或者它与您的
样式有关
我希望我没有误解您的问题,但我认为没有必要绑定TextBlock.Width
此xaml似乎工作正常:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="270"
Width="2*" />
<ColumnDefinition MinWidth="160"
Width="*" />
<ColumnDefinition MinWidth="160"
Width="*" />
<ColumnDefinition MinWidth="160"
Width="*" />
</Grid.ColumnDefinitions>
<!-- We bind the width of the textblock to the width of this border to make sure things resize correctly.
It's important that the margin be set to 1 larger than the margin of the textblock or else you'll end
up in an infinate loop -->
<Border Grid.Column="0"
BorderThickness="0,0,1,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Border>
<TextBlock Text="Col 1 Row 1" />
</Border>
<TextBlock TextWrapping="Wrap"
Text="gfege dfh lh dfl dhliöslghklj h lglsdg fklghglfg flg lgheh" />
</StackPanel>
<StackPanel Grid.Row="1">
<Border>
<TextBlock Text="Col 1 Row 2" />
</Border>
<TextBlock Text="Massor av text som blir en wrappning i slutändan hoppas jag"
TextWrapping="Wrap" />
</StackPanel>
</Grid>
</Border>
</Grid>
我刚刚删除了宽度绑定,添加了TextWrapping(您可能有一种风格),还删除了名为“FirstBorder”的边框。我希望我没有误解您的问题,但我认为没有必要绑定TextBlock.width 此xaml似乎工作正常:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="270"
Width="2*" />
<ColumnDefinition MinWidth="160"
Width="*" />
<ColumnDefinition MinWidth="160"
Width="*" />
<ColumnDefinition MinWidth="160"
Width="*" />
</Grid.ColumnDefinitions>
<!-- We bind the width of the textblock to the width of this border to make sure things resize correctly.
It's important that the margin be set to 1 larger than the margin of the textblock or else you'll end
up in an infinate loop -->
<Border Grid.Column="0"
BorderThickness="0,0,1,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Border>
<TextBlock Text="Col 1 Row 1" />
</Border>
<TextBlock TextWrapping="Wrap"
Text="gfege dfh lh dfl dhliöslghklj h lglsdg fklghglfg flg lgheh" />
</StackPanel>
<StackPanel Grid.Row="1">
<Border>
<TextBlock Text="Col 1 Row 2" />
</Border>
<TextBlock Text="Massor av text som blir en wrappning i slutändan hoppas jag"
TextWrapping="Wrap" />
</StackPanel>
</Grid>
</Border>
</Grid>
我刚刚删除了宽度绑定,添加了TextWrapping(您可能在某个样式中使用过),还删除了名为“FirstBorder”的边框。我不想回答我自己的问题,但似乎我已经发现我做得不对。既然已经很久没有人问我最初的问题了,我记不起我试图采取的每一步,但这就是我所知道的。每个文本块上的样式设置如下:
<Style x:Key="DetailText" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Margin" Value="5,5,5,5" />
</Style>
当时,我假设这不会产生期望的结果,因此我必须将textblock的宽度绑定到列的宽度。在今天的游戏中,我将样式更改为以下样式(注意不同的水平对齐方式),并删除了绑定,发现我的问题已经解决:
<Style x:Key="DetailText" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Margin" Value="5,5,5,5" />
</Style>
我不想回答我自己的问题,但似乎我已经发现我做错了什么。既然已经很久没有人问我最初的问题了,我记不起我试图采取的每一步,但这就是我所知道的。每个文本块上的样式设置如下:
<Style x:Key="DetailText" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Margin" Value="5,5,5,5" />
</Style>
当时,我假设这不会产生期望的结果,因此我必须将textblock的宽度绑定到列的宽度。在今天的游戏中,我将样式更改为以下样式(注意不同的水平对齐方式),并删除了绑定,发现我的问题已经解决:
<Style x:Key="DetailText" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Margin" Value="5,5,5,5" />
</Style>
我想我应该