2016-07-05 5 views
0

私には、エキスパンダーと他のほとんどのコントロールを含むユーザーコントロールがあります。WPFカスタムプロパティの動的値

ユーザーコントロールには、エクスパンダ専用の背景を実際に設定するカスタムの「XBackground」プロパティがあります。

public Brush XBackground 
    { 
     get 
     { 
      return expander.Background; 
     } 
     set 
     { 
      expander.Background = value; 
     } 
    } 

私は私のユーザーコントロールを使用すると、背景がだけでなく、動的、静的に設定することができます。デバッガでは、動的リソースを介してDependencyPropertyのみを設定できると記載されています。そしてここで私は立ち往生している。私はXBackgroundプロパティに依存プロパティを登録しようとしましたが、 "A 'DynamicResourceExtension'はDependencyObjectのDependencyPropertyにしか設定できないというエラーが表示されます。ここで

は、依存関係プロパティを登録する私の試みです:

public static readonly DependencyProperty BcgProperty = DependencyProperty.Register("XBackground", typeof(Brush), typeof(Expander)); 

答えて

0

: これを試してみてください。また、プロパティーラッパーでGetValueSetValueを呼び出す必要があります。 PropertyChangedCallback

public partial class YourUserControl : UserControl 
{ 
    ... 

    public Brush XBackground 
    { 
     get { return (Brush)GetValue(XBackgroundProperty); } 
     set { SetValue(XBackgroundProperty, value); } 
    } 

    public static readonly DependencyProperty XBackgroundProperty = 
     DependencyProperty.Register(
      "XBackground", 
      typeof(Brush), 
      typeof(YourUserControl), 
      new PropertyMetadata(null, XBackgroundPropertyChanged)); 

    private static void XBackgroundPropertyChanged(
     DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     var userControl = (YourUserControl)obj; 
     userControl.expander.Background = (Brush)e.NewValue; 
    } 
} 

代替パンダさんをバインドするために、次のようになります。

それに加えて、あなたはまた、プロパティ値の変更について通知を受けるためのプロパティメタデータ、とPropertyChangedCallbackを登録するには持っていますXAMLでのUserControlのXBackgroundプロパティにBackgroundプロパティ:

<UserControl ...> 
    ... 
    <Expander Background="{Binding XBackground, 
     RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/> 
    ... 
</UserControl> 
+0

はい!私は 'XBackgroundPropertyChanged'の内部から自分のコントロールにアクセスする方法を知らなかった、ありがとう! – Adder

0

マイナー間違い:

のpublic static読み取り専用のDependencyProperty BcgProperty = DependencyProperty.Register( "XBackground"、typeof演算(ブラシ)、typeof演算(YourCustomUserControl));

フルバージョン:レジスタの

public static readonly DependencyProperty XBackgroundProperty 
      = DependencyProperty.Register("XBackground", typeof(Brush), typeof(YourCustomUserControl)); 
public Brush XBackground 
{ 
    get { return (Brush) GetValue(XBackgroundProperty); } 
    set { SetValue(XBackgroundProperty, value); } 
} 
0

所有者クラスにはパンダが、あなたのDependencyPropertyが登録されているクラスの名前ではありません。あなたがRegisterメソッドの引数としてownerTypetypeof(Expander)を使用しますが、代わりにユーザーコントロールの種類はいけません

public Brush XBackground 
    { 
     get { return (Brush)GetValue(XBackgroundProperty); } 
     set { SetValue(XBackgroundProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for XBackground. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty XBackgroundProperty = 
     DependencyProperty.Register("XBackground", typeof(Brush), typeof(/typeof your UserControl goes here/), new PropertyMetadata(null)); 
+0

まあ、エラーが私のコントロール今なくなっているが、透明ですが、色は適用されません。 – Adder

+1

xamlを表示すると、バインディングのソースが正しく設定されていないように見えます – bakala12

+0

ソースは問題なく、他のすべてのプリセットプロパティで動作します。 'XBackground =" {DynamicResource ResourceKey = bcgResource} "' – Adder

関連する問題