Resourcesに定義した文字列を XAML で使う方法

Button や Label に表示するテキストとか、DataGrid の列ヘッダのタイトルとか、XAML に日本語で直書きしています。日本以外でアプリをリリースすることは多分ないでしょうし。

でも、将来何があるかわからないし、ローカライズすることを考えた方がいいかも。そうなると Properties.Resources に定義した文字列を XAML で使う必要がありますね。


まずプロジェクトのプロパティを開いてリソースタブを選び、右上のアクセス修飾子をpublicにしておきます。
f:id:griefworker:20100928161300j:image

あとは XAML の中で x:Static マークアップ拡張を使って指定するだけ。

<Window x:Class="ResourcesSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prop="clr-namespace:ResourcesSample.Properties"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Label Content="{x:Static prop:Resources.LabelText}"/>
        <Button Content="{x:Static prop:Resources.ButtonText}"/>
    </StackPanel>
</Window>


もし、アプリの海外展開なんて野望を考えてるなら、文字列はリソースにしておいたほうがいいです。英語が得意でない私には夢のまた夢ですけど。