簡體   English   中英

將轉換器應用於Blend中的Number時,設計視圖在TextBlock中顯示`System.Object`

[英]Design view shows `System.Object` in TextBlock when applying a converter to a Number in Blend

在Microsoft Blend for Visual Studio(Express 2013和Community 2015 RC)中,我創建了一些示例數據,其中包含一個由數字和字符串組成的項集合。

數據樣本

amount被綁定到TextBlock

<TextBlock Text="{Binding amount}" />

數字顯示正常,但我希望它們格式化為顯示2位小數的字符串。 由於StringFormat 不適用於Universal Apps ,我嘗試添加一個嘗試實現相同功能的轉換器

public class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return string.Format(parameter as string, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}

我為它添加了一個資源:

<Page.Resources>
    <ResourceDictionary>
        <local:StringFormatConverter x:Name="StringFormat"/>
    </ResourceDictionary>
</Page.Resources>

我調整了TextBlock的綁定:

<TextBlock Text="{
    Binding amount, 
    Converter={StaticResource StringFormat}, 
    ConverterParameter='{}{0:f2}'}" />

但是,當我這樣做時,視圖面板顯示每個金額的System.Object

即使我添加一個什么都不做的轉換器,只返回值的值或toString() ,我得到相同的結果! 這只發生在樣本數據類型Number

如何將綁定格式化為Blend中的示例數據集中的數字?

不知道這些年后這將會有多大幫助,但為什么不在代碼背后做到這一點? 您只需在完成必要的轉換后更新TextBlock的“Text”屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM