简体   繁体   English

WPF-使用StringFormat时无法右对齐TextBlock

[英]WPF - Can't right-align a TextBlock when using StringFormat

I have a TextBlock within a GridViewColumn. 我在GridViewColumn中有一个TextBlock。 I format to currency using StringFormat. 我使用StringFormat格式化为货币。 This causes the field to be left aligned. 这将导致字段左对齐。 I can't get it right aligned. 我无法正确对齐。 Tried HorizontalAlignment and TextAlignment but nothing works. 尝试了Horizo​​ntalAlignment和TextAlignment,但是没有任何效果。

<TextBlock HorizontalAlignment="Right" Text="{Binding Path=Amount, StringFormat={}{0:€ # ##0}}" />
<TextBlock TextAlignment="Right" Text="{Binding Path=Amount, StringFormat={}{0:€ # ##0}}" />

If I do this from this post , it works but right aligns all columns. 如果我从这篇文章中做到这一点 ,它可以工作,但可以使所有列正确对齐。 I want numbers right aligned, dates centre and text left. 我希望数字右对齐,日期中心和文本左。

<ListView.Resources>  
    <Style TargetType="ListViewItem">  
        <Setter Property="HorizontalContentAlignment" Value="Right" />  
    </Style>
</ListView.Resources>

Any clues please? 有什么线索吗?

Set HorizontalContentAlignment to Stretch . HorizontalContentAlignment设置为Stretch This will work: 这将起作用:

<ListView>
   <ListView.Resources>
      <Style TargetType="ListViewItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
   </ListView.Resources>
   <ListView.View>
      <GridView>
         <GridViewColumn>
            <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <TextBlock HorizontalAlignment="Right"
                       Text="{Binding Path=Amount, StringFormat={}{0:€ # ##0}}"/>
               </DataTemplate>
            </GridViewColumn.CellTemplate>
         </GridViewColumn>
      </GridView>
   </ListView.View>
 </ListView>

Change your resource to HorizontalContentAlignment to Stretch, this will allow the contents of each cell in the ListView to take up the entire space. 将您的资源更改为Horizo​​ntalContentAlignment到Stretch,这将允许ListView中每个单元格的内容占据整个空间。 Then your HorizontalAlignment attributes will work. 然后,您的Horizo​​ntalAlignment属性将起作用。

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

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