我需要在TextBlock之后立即添加图像。
当文本在第二行显示时文本不适合宽度时,TextBlock具有特定的宽度。
图像也应放在第二行文本行的末尾。
知道如何为我的DataTemplate更改xaml吗?
<DataTemplate x:Key="CategoriesUnselectedDataTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Name="TitleCategory"
Width="360"
TextWrapping="Wrap"
Text="{Binding CapitalizedDescription, FallbackValue=CategoryName}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="30"
Margin="0,10"/>
<Image Source="Assets/common/right_grey.png"
Width="20"
Height="20"
Visibility="Visible"
HorizontalAlignment="Left"
VerticalAlignment="Center"
d:LayoutOverrides="Height"
Grid.Column="1"
Margin="10,0,0,0"/>
</Grid>
</DataTemplate>
答案 0 :(得分:1)
根据您的应用需求尝试更改数据模板,
<DataTemplate x:Key="CategoriesUnselectedDataTemplate">
<Border Margin="2" Padding="2" BorderThickness="2" CornerRadius="5" Background="#FFFFFFFE">
<WrapPanel Width="185" Height="50">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" FontFamily="Arial" FontWeight="Black" FontSize="14" />
</StackPanel>
<Image Source="pack://application:,,,/Image/sample.png" Stretch="None" Margin="1,1,5,1"/>
</WrapPanel>
</Border>
</DataTemplate>
答案 1 :(得分:1)
您可以尝试使用Inlines
TextBlock
属性。
将TextBlock XAML更改为:
<TextBlock
Name="TitleCategory"
Width="360"
HorizontalAlignment="Left"
Margin="0,10"
VerticalAlignment="Center"
FontSize="30"
TextWrapping="Wrap">
<TextBlock.Inlines>
<Run Text="{Binding CapitalizedDescription, FallbackValue=CategoryName}"/>
<Image Source="Assets/common/right_grey.png"
Width="20"
Height="20"
Visibility="Visible"
HorizontalAlignment="Left"
VerticalAlignment="Center"
d:LayoutOverrides="Height"
Grid.Column="1"
Margin="10,0,0,0"/>
</TextBlock.Inlines>
</TextBlock>
请注意,Binding只能在WPF 4.0上运行Run.Text属性。
答案 2 :(得分:1)
为什么不直接使ColumnDefinition Width =“Auto”