2011-06-24 23 views
2

アドバイスが必要です(実際のプロジェクトよりも優れています) - WPFで視覚的インセレンスを実行する最善の方法は何ですか?WPFビジュアル継承

もっと具体的には:ステータスバーでウィンドウビューを埋めるにはどうすればいいですか?

xamlファイルを別のファイルに埋め込む方法はありません。次に、User Control MyStatusbarを作成して、すべてのページに貼り付けますか?

ベースウィンドウ用のスタイルテンプレートを作成し、スタイルの無関係を使用することは可能ですが、これは単純な視覚的プロパティ(色、サイズ)に対してのみ可能です。 2番目のアイデアはベースのDataTemplateを作成することですが、継承はありません。

P.S. WinFormsでは、ステータスバーとロジックを持つ基本フォームがあります。プロパティを追加した後、子フォームでプロパティを使用するのは非常に簡単です。

public string StatusbarText {set{baseStatusbar.Text = value;}} 

さらに、ステータスバーで継承を継承しています。

私はWPFで論理をどのようにするか知っていますが、ビジュアライゼーションとは何かを知っています。

答えて

1

を見て見て、スタイルとテンプレートにデータバインディングでこれのViewModelプロパティを使用することができます。代わりに、カスタムスタイルのウィンドウを使用することもできます。唯一の質問は、ステータスバーアイテムをスタイルに渡す方法です。そのためにはattached propertiesを使用できます。

このルートに進むと、ControlTemplateを完全に再定義する必要があるため、カスタムスタイルをデフォルトのものから継承することはできません。あなたは上記のスタイルを使用している場合は、ステータスバーに表示したい項目のリストであることをWindow.Tagプロパティを設定することができます

<ControlTemplate x:Key="WindowTemplateKey" 
       TargetType="{x:Type Window}"> 
    <Border Background="{TemplateBinding Background}" 
      BorderBrush="{TemplateBinding BorderBrush}" 
      BorderThickness="{TemplateBinding BorderThickness}"> 
     <Grid> 
      <AdornerDecorator> 
       <DockPanel> 
        <StatusBar DockPanel.Dock="Bottom" ItemsSource="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
        <ContentPresenter/> 
       </DockPanel> 
      </AdornerDecorator> 

      <ResizeGrip x:Name="WindowResizeGrip" 
         HorizontalAlignment="Right" 
         VerticalAlignment="Bottom" 
         Visibility="Collapsed" 
         IsTabStop="false"/> 
     </Grid> 
    </Border> 
    <ControlTemplate.Triggers> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="Window.ResizeMode" 
          Value="CanResizeWithGrip"/> 
       <Condition Property="Window.WindowState" 
          Value="Normal"/> 
      </MultiTrigger.Conditions> 
      <Setter TargetName="WindowResizeGrip" 
        Property="Visibility" 
        Value="Visible"/> 
     </MultiTrigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 


<Style x:Key="{x:Type Window}" 
     TargetType="{x:Type Window}"> 
    <Setter Property="Foreground" 
      Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
    <Setter Property="Background" 
      Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Window}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <AdornerDecorator> 
         <DockPanel> 
          <StatusBar DockPanel.Dock="Bottom" ItemsSource="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
          <ContentPresenter/> 
         </DockPanel> 
        </AdornerDecorator> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Window.ResizeMode" 
       Value="CanResizeWithGrip"> 
      <Setter Property="Template" 
        Value="{StaticResource WindowTemplateKey}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

:ウィンドウのスタイルは次のようになります。このアプローチの最大の問題は、ステータスバーの外観をカスタマイズできるように、StatusBar.ItemContainerStyleのような属性に添付プロパティを追加する必要があることです。

DataTemplateを使用する場合も同様です。ですから、ステータスバーに単一のテキストだけが必要な場合は、上記のControlTemplatesで次のコードを使用し、Window.Tagを文字列に設定します(または添付プロパティを使用します)。

<StatusBar DockPanel.Dock="Bottom"> 
    <StatusBarItem Content="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" /> 
</StatusBar> 
+0

はアイデアを得ました。ありがとう。 はまた、のContentPresenterについてmensionすることが重要である: <のContentPresenter \t \t \t \t \tコンテンツ= "{TemplateBindingの内容}" \t \t \t \t \t証拠金= "0" 幅= "オート" /> いくつかがありますCatelを使った例http://www.codeproject.com/KB/WPF/Catel_Part5.aspx – Artru

1

それはあなたがStatusbarTextプロパティで、ベースビューモデルを作成した後、ベースのViewModelから継承することができますWPF Apps With The Model-View-ViewModel Design Pattern

を見て、MVVMを使用することをお勧めします。

次に、あなたはCustomize Data Display with Data Binding and WPF

はまた、あなたは確かにStatusbarTextプロパティを追加するカスタムウィンドウコントロールを作成することができthis question

+0

それは方法の一つだとWPFで選びだしする可能性が高く、問題は(継承に類似)の視覚的な分離を行うことにした – Artru