2012-01-20 15 views
2

私のフォームには2つのテキストブロックがあります。このような 何か:設定からテキストブロックコンテンツを文字列に設定します

<TextBlock Name="applicationname" Text="{binding applicationname?}"/> 
<TextBlock Name="applicationname" Text="{binding settings.stringVersionNumber}"/> 

iは、自動的にアプリケーション名を表示するには、最初のTextBlockコンテンツやアプリケーションの設定に保存された文字列を表示するために他のものを設定したいと思います..私は

を変更する必要があります値は "wpfコードビハインド"を使うか、xamlで直接textblocksをバインドできますか?

答えて

5

あなたがデータを直接XAMLで、アプリケーションの設定などの静的プロパティにバインドすることができます。

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:WpfApplication1" 
     xmlns:p="clr-namespace:WpfApplication1.Properties" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <StackPanel> 
      <TextBlock Text="{Binding Source={x:Static l:MainWindow.AssemblyTitle}}"/> 
      <TextBlock Text="{Binding Source={x:Static p:Settings.Default}, Path=VersionNumber}"/> 
     </StackPanel> 
    </Grid> 
</Window> 

... WpfApplication1があなたの名前空間で、 VersionNumberは、アプリケーションの設定で定義された文字列です。我々は MainWindowクラスにいくつかのコードビハインド必要があります

アセンブリのタイトルを取得するには、:

public static string AssemblyTitle 
{ 
    get 
    { 
     return Assembly.GetExecutingAssembly() 
         .GetCustomAttributes(typeof(AssemblyTitleAttribute), false) 
         .Cast<AssemblyTitleAttribute>() 
         .Select(a => a.Title) 
         .FirstOrDefault(); 
    } 
} 

P.S. 2つの要素に同じ名前を割り当てることはできません。

+0

大変ありがとうございます...アプリケーション名(設定に保存されていない)を表示する方法は、例のように簡単ですか? – lebhero

+0

あなたは '[assembly:AssemblyTitle]'属性で指定された値を意味しますか? – Douglas

+0

はい正確にこの値 – lebhero

関連する問題