2012-05-07 26 views
2

WPF ItemContainerStyle用の再利用可能なテンプレートを作成しようとしています。WPFスタイルにプロパティを渡す方法

このテンプレートは、TabControlのアイテムの表示方法を変更します。 このテンプレートは、アプリケーションのいくつかの場所で使用するためのものです。

それぞれの場所で使用されていますが、別のパラメータを渡したいと思っています。たとえば :項目のボーダーの余白を変更するには:

<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}"> 

       <Setter Property="Margin" Value="10,0"/> 

       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type TabItem}"> 
          <Grid SnapsToDevicePixels="true"> 
           <Border x:Name="Bd" Width="80" 
            Background="Gray" 
            Margin="{TemplateBinding Margin}"> 
            <ContentPresenter x:Name="Content" 
             ContentSource="Header" /> 
           </Border> 
          </Grid> 
          <ControlTemplate.Triggers> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
... 
<TabControl ItemContainerStyle="{DynamicResource TabItemStyle1}"> 

スタイルは私のようなものを書きたい使用されている場所では:

ItemContainerStyle="{DynamicResource TabItemStyle1 Margin='5,0'}" 

または

<TabControl Margin="78,51,167,90" ItemContainerStyle="{DynamicResource TabItemStyle1}" 
      ItemContainerStyle.Margin="5,0"> 

このテンプレートは、さまざまなマージンの異なる場所で使用することを目指しています。 これを行う方法はありますか?

はあなたに

答えて

2

OKでリソースを再定義することで、私がこれを行う方法を見つけた:私はそれを行う方法を説明するブログ記事を書きましたデイブの助け。

解決策は、派生テンプレートを作成し、プロパティを設定することです。 元のテンプレートをこの方法で再利用できます。

<Style x:Key="TabItemStyle2" TargetType="{x:Type TabItem}" 
    BasedOn="{StaticResource TabItemStyle1}"> 
    <Style.Setters> 
     <Setter Property="Margin" Value="40,0"></Setter> 
    </Style.Setters> 
</Style> 

と派生スタイルにTabControlののItemContainerStyleを設定します。私の場合は

<TabControl ItemContainerStyle="{DynamicResource TabItemStyle2}"> 
0

それはあなたが表示され、テンプレートでその値に(データ)バインドするオブジェクト/のviewmodelsにMarginプロパティを追加することである解決の道をありがとうございます。

私が知る限り、パラメータ化されたスタイル/テンプレートのサポートはありません。

4

添付のプロパティで行うことができます。

http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/

別のオプションは、DynamicResourceを使用し、派生スタイル

+0

ありがとうThomas、あなたの最初の答えははっきりとエレガントですが、私は追加のC#コードを書くことなくこれを行う方法を望んでいました。上記の私の答えはこれを行うが、再び小さな静的クラスを書くことは大したことではなく、もっとエレガントになるかもしれない。ありがとうございました。 –

+0

ところで - 私の答え(上記)は静的なプロパティを使用しています。 –

3

を私は適用したテンプレート(私はちょうどセッターを使用することができませんでした)の深いいくつかのパラメータを変更しなければなりませんでした。 そして、私はビジュアルツリーを横断するいくつかのクラスをコーディングしたり、変更を行うために添付プロパティを登録したりしたくありませんでした。

ただし、基本スタイル内でリソースを定義し、これらの値を派生定義で上書きすることは可能です。したがって、元の例では、これは次のようになります。

<Style x:Key="AbsTabItemStyle" TargetType="{x:Type TabItem}"> 
    <!-- Override these default values in derived style definitions --> 
    <Style.Resources> 
     <s:Double x:Key="GridBorderMargin">10</s:Double> 
     <Color x:Key="GridBorderColor">Grey</Color> 
    </Style.Resources> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TabItem}"> 
       <Grid SnapsToDevicePixels="true"> 
        <Border x:Name="Bd" 
          Width="80" 
          Background="{DynamicResouces GridBorderColor}" 
          Margin="{DynamicResouces GridBorderMargin}" 
          > 
         <ContentPresenter x:Name="Content" 
          ContentSource="Header" /> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="BigMarginTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource AbsTabItemStyle}"> 
    <!-- Set different values in this derived style definition --> 
    <Style.Resources> 
     <s:Double x:Key="GridBorderMargin">20</s:Double> 
    </Style.Resources> 
</Style> 

<Style x:Key="RedTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource AbsTabItemStyle}"> 
    <!-- Set different values in this derived style definition --> 
    <Style.Resources> 
     <c:Color x:Key="GridBorderColor">Red</Color> 
    </Style.Resources> 
</Style> 
関連する問題