< メモ: コマンド | C# には friend キーワードがない >

November 13, 2008

Binding の StringFormat プロパティが反映されない

.NET Frameworks 3.5 SP1 より、BindingBase に StringFormat プロパティが追加されて、話題になりました。が、こんなの書いてみてもちっともフォーマットされず、悩みました。

<Window x:Class="StringFormatTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Title="Window1">
    <Window.Resources>
        <ObjectDataProvider x:Key="DayOfWeek" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="System:DayOfWeek"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource DayOfWeek}, StringFormat={}{0}!}"/>
</Window>

これは、ItemsControl クラスの ItemStringFormat プロパティの値のほうが優先されるためです。こちらは、意図したとおりに「!」が追加されて表示されます。

<ListBox ItemsSource="{Binding Source={StaticResource DayOfWeek}}" ItemStringFormat="{}{0}!"/>

その他、ContentControl を継承するコントロールの場合 ContentStringFormat プロパティが、HeaderedContentControl や HeaderedItemsControl の場合、HeaderStringFormat プロパティが存在して、Binding に設定した StringFormat より優先されるようです。

トラックバック

このエントリーにトラックバック:
http://frog.raindrop.jp/cgi-bin/mt/mt-tb.cgi/2239

コメント

コメントする

※ コメントスパム対策のため、コメント本文はおはよう、こんにちわ、こんばんわのいずれかより始めるようにしてください。

name:
email:

※ 必要ですが、表示しません。

url:
情報を保存する ?