如何设置文本块大小以始终适合绑定数据?
How to set Textblock size to always fit the bound data?
我正在开发 WPF GUI。我有很多视图,每个视图都有几个 TextBlocks,我试图摆脱烦人的设计(我知道红色很烦人,但事实并非如此)所以我将用几个示例文本块来解释,这是它的 XAML :
有时有数据"Binding behind",有时是一个单词,有时是几个单词,有时根本没有数据。无论数据是什么,如何将它们并排放置?
<TextBlock x:Name="sessionNameTextBlock" Margin="0,0,1123,9.8" Text="{Binding CurrentSessionName}" VerticalAlignment="Bottom" FontSize="48" TouchUp="sessionNameTextBlock_TouchUp" MouseUp="sessionNameTextBlock_MouseUp" Height="57" Background="#FFE81616"/>
<Border x:Name="SessionEditorButton" BorderBrush="Black" BorderThickness="0" Margin="48,0,1027,9.8" MouseUp="SessionEditorButton_MouseUp" TouchUp="SessionEditorButton_TouchUp" Background="#FFF91515" Height="57" VerticalAlignment="Bottom">
<Image x:Name="image" Margin="10,10,9.8,10.6" Source="/EZ3D;component/Resources/ez3d_edit.png"/>
</Border>
<TextBlock x:Name="sessionCommentsTextBlock" Margin="144,0,975,9.8" Text="{Binding CurrentSessionComments}" MouseUp="sessionCommentsTextBlock_MouseUp" TouchUp="sessionCommentsTextBlock_TouchUp" FontSize="18.667" Background="#FFF41515" Height="57" VerticalAlignment="Bottom"/>
当前状态是这样的:
或者如果我手动调整它的大小:
我想要的是:
------------愿景----------------
执行一的内容是'S'和'E',然后执行二的内容是"Whatever... etc"和"Comments ... etc" 所以TextBlock会根据内容调整大小。
因此,以下 XAML 的行为符合您对我 的要求。
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="tb1" Text="Test" />
<Border x:Name="SessionEditorButton" BorderBrush="Black" BorderThickness="0" Background="#FFF91515" Height="57" VerticalAlignment="Bottom">
<Image x:Name="image" Margin="10,10,9.8,10.6" />
</Border>
<TextBlock x:Name="tb2" Text="Test 2"/>
</StackPanel>
承认这在一个非常人为的例子中有效,但它归结为 TextBlock
s will 自动调整自己的大小 if 他们的容器有 space 允许它。在这种情况下,我的 StackPanel
填充了 window,因此 TB 有足够的空间来调整大小。您可能需要查看您的容器层次结构以确定哪个容器限制了您的文本块。
我正在开发 WPF GUI。我有很多视图,每个视图都有几个 TextBlocks,我试图摆脱烦人的设计(我知道红色很烦人,但事实并非如此)所以我将用几个示例文本块来解释,这是它的 XAML :
有时有数据"Binding behind",有时是一个单词,有时是几个单词,有时根本没有数据。无论数据是什么,如何将它们并排放置?
<TextBlock x:Name="sessionNameTextBlock" Margin="0,0,1123,9.8" Text="{Binding CurrentSessionName}" VerticalAlignment="Bottom" FontSize="48" TouchUp="sessionNameTextBlock_TouchUp" MouseUp="sessionNameTextBlock_MouseUp" Height="57" Background="#FFE81616"/>
<Border x:Name="SessionEditorButton" BorderBrush="Black" BorderThickness="0" Margin="48,0,1027,9.8" MouseUp="SessionEditorButton_MouseUp" TouchUp="SessionEditorButton_TouchUp" Background="#FFF91515" Height="57" VerticalAlignment="Bottom">
<Image x:Name="image" Margin="10,10,9.8,10.6" Source="/EZ3D;component/Resources/ez3d_edit.png"/>
</Border>
<TextBlock x:Name="sessionCommentsTextBlock" Margin="144,0,975,9.8" Text="{Binding CurrentSessionComments}" MouseUp="sessionCommentsTextBlock_MouseUp" TouchUp="sessionCommentsTextBlock_TouchUp" FontSize="18.667" Background="#FFF41515" Height="57" VerticalAlignment="Bottom"/>
当前状态是这样的:
或者如果我手动调整它的大小:
我想要的是:
------------愿景----------------
执行一的内容是'S'和'E',然后执行二的内容是"Whatever... etc"和"Comments ... etc" 所以TextBlock会根据内容调整大小。
因此,以下 XAML 的行为符合您对我 的要求。
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="tb1" Text="Test" />
<Border x:Name="SessionEditorButton" BorderBrush="Black" BorderThickness="0" Background="#FFF91515" Height="57" VerticalAlignment="Bottom">
<Image x:Name="image" Margin="10,10,9.8,10.6" />
</Border>
<TextBlock x:Name="tb2" Text="Test 2"/>
</StackPanel>
承认这在一个非常人为的例子中有效,但它归结为 TextBlock
s will 自动调整自己的大小 if 他们的容器有 space 允许它。在这种情况下,我的 StackPanel
填充了 window,因此 TB 有足够的空间来调整大小。您可能需要查看您的容器层次结构以确定哪个容器限制了您的文本块。