0

依存関係プロパティを作成すると、それらのほとんどがUserControlのプロパティの名前(背景、幅など)と競合することがわかりました。そのため、私の戦略では、すべてのカスタムプロパティの前に「The」を付けます。

  • 背景

警告を取り除く「new」キーワードを使用してみましたが、実行時に競合が発生します。

カスタムユーザーコントロールのDependencyPropertiesのより良い命名戦略に出くわした人はいますか?

public partial class SmartForm : UserControl
{
    public SmartForm()
    {
        InitializeComponent();
        DataContext = this;

        TheBackground = "#FFD700";
    }

    #region DependencyProperty: TheBackground
    public string TheBackground
    {
        get
        {
            return (string)GetValue(TheBackgroundProperty);
        }
        set
        {
            SetValue(TheBackgroundProperty, value);
        }
    }

    public static readonly DependencyProperty TheBackgroundProperty =
        DependencyProperty.Register("TheBackground", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata());
    #endregion
}
4

2 に答える 2

5

UserControlにbackgroundプロパティがある場合、なぜ別のプロパティを追加する必要があるのですか?

この新しい背景の背景は何ですか?「」?いいえ?それでは、どのような背景を制御しますか?

次の文を完成させます:「これはXXXXコントロールの背景色です。」

プロパティ名はXXXXBackgroundになります。

于 2009-05-20T14:27:50.223 に答える
0

オーバーライドしたいということですか?

私の場合、WidthをDepedencyPropertyとして設定しません

私のカスタムコントロールには次のものがあります。

    public double Width
    {
        get
        {
            if (_backgroundImage != null)
                return _backgroundImage.Width;
            else
                return double.NaN;
        }
        set
        {
            if (_backgroundImage != null)
                _backgroundImage.Width = value;
        }
    }

コンパイラは警告を表示しますが、すべてが機能しているようです。

于 2009-05-22T22:06:27.527 に答える