2

ユーザーコントロールにカスタム属性を追加して、UserControlがさまざまなオプションを表示(またはオン/オフ)するときにその属性を使用するようにしたい

すなわち

<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" />

どうすればよいですか?

4

2 に答える 2

3

必要なのは依存関係プロパティです

public class UC_TitleBar : UserControl
{
    public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.Register("ShowCloseButton", 
                                                    typeof(Boolean), typeof(UC_TitleBar), new FrameworkPropertyMetadata(false));
    public bool ShowCloseButton
    {
        get { return (bool)GetValue(ShowCloseButtonProperty); }
        set { SetValue(ShowCloseButtonProperty, value); }
    }
}
于 2013-03-22T09:38:45.467 に答える
0
    //add dependency property
    public static DependencyProperty MyTestProperty;

    //init dependency property in static control constructor
    static MyControl()
    {
         var myTestPropertyMetadata = new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender, MyTestPropertyChanged);


                MyTestProperty= DependencyProperty.Register("MyTest",
                                                       typeof(string),
                                                       typeof(MyControl),
                                                       myTestPropertyMetadata );
    }

   //implement property 
    public String MyTest
    {
        get { return (String)GetValue(MyTestProperty); }
        set
        {
            SetValue(MyTestProperty, value);
        }
    }

   //using in xaml
   <MyControls:MyControl MyTest="dfdsf" />

MSDN で依存関係プロパティの詳細を読む

于 2013-03-22T09:42:17.793 に答える