c# - 为什么我的 StringFormat 在 WPF 中不起作用

标签 c# wpf xaml string-formatting

<分区>

我在 SO 上看到过一些这样的问题,但没有一个答案对我有用!这是我的删节代码:

<Grid Margin="10,4,2,3">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>

    <Image Name="myImage" />

    <WrapPanel Orientation="Vertical" Grid.Column="1" >
        <Label x:Name="labelDimensions" Content="Image Dimensions" />
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3*" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="3*" />
            </Grid.ColumnDefinitions>

            <Label Grid.Column="0" 
                   x:Name="imageWidth" 
                   Content="{Binding Path=ActualWidth, 
                             ElementName=myImage, 
                             StringFormat={}{0:1234.5}}" />
            <Label Grid.Column="1" x:Name="label3" Content=" x " />
            <Label Grid.Column="2" 
                   x:Name="imageHeight" 
                   Content="{Binding Path=ActualHeight, 
                             ElementName=myImage, 
                             StringFormat={}{0:1234.5}}" />
        </Grid>
    </WrapPanel>

</Grid>

我希望在“图像尺寸”标签下方看到“641.3 x 480.0”之类的内容,但无论我做什么,它都会显示“641.30000000 x 480”之类的内容。

我有像 {0:1234.5} 和 {0:#,#.#} 这样的格式,但没有任何效果。我也尝试过 ConentStringFormat 作为建议的一个 SO 答案,但它甚至没有编译。

如有任何建议,我们将不胜感激。

最佳答案

改用ContentStringFormat

StringFormat 仅在绑定(bind)到 String 类型的属性时使用。 LabelContent 属性是对象类型,如您所见here , 所以 StringFormat 将不起作用。

关于c# - 为什么我的 StringFormat 在 WPF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753221/

相关文章:

c# - LINQ join 行为异常

c# - 试图读写 protected 内存

sql-server - 我收到错误 “Incorrect syntax near the keyword ',其中“”

c# - 如何正确创建单元格格式?

c# - 两个系统之间使用私钥进行 RSA 解密

c# - 在 WPF 中应用程序启动时应默认选择 ListView 项

c# - 更改 TextBox 文本时 ViewModel 属性未更新

wpf - 如何创建棋盘格图案?

c# - 在检查取消时让后台工作人员进入休眠状态

c# - 数据绑定(bind)到计算字段