PG.Lib

アプリ開発関連とかガジェット関連とか

Xamarin Forms Data Binding で フォーマットを指定する

今回は前回のデータバインディングした値に、フォーマットを指定して表示するようにしてみます。

XAML

<Grid Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"
      Style="{StaticResource baseGrid}">
    <Label
       Text="{Binding stringValue, StringFormat='初期値は {0}'}" FontSize="Large">
    </Label>
</Grid>

<Grid Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="6"
      Style="{StaticResource baseGrid}">
    <Label
       Text="{Binding intValue, StringFormat='intValue {0}'}" FontSize="Large">
    </Label>
</Grid>

<Grid Grid.Column="5" Grid.ColumnSpan="2" Grid.Row="1"
      Style="{StaticResource baseGrid}">
    <Label
       Text="{Binding dateValue, StringFormat='今日は {0:yyyy/MM/dd}'}" FontSize="Large">
    </Label>
</Grid>

<Grid Grid.Column="5" Grid.ColumnSpan="2" Grid.Row="6"
      Style="{StaticResource baseGrid}">
    <Label
       Text="{Binding decValue, StringFormat='decValue {0:#,0}'}" FontSize="Large">
    </Label>
</Grid>

Binding の中に StringFormat を追記します。

StringFormatの書式は以下のMSDNが参考になるかと思います。

カスタム数値書式指定文字列

カスタム日時書式指定文字列

他は前回のデータバインディングと同様です。

実行結果

f:id:fxxk_authority:20170501160811p:plain

こんな感じで表示されました。

例によって、Xamarin.Forms Previewer には出てこないので、実行しながら内容を確認することになりそう。