3

ActualWidthユーザー コントロールのコンポーネントの 1 つのプロパティをユーザーに公開するにはどうすればよいですか?

新しい依存関係プロパティとバインディングを作成して通常のプロパティを公開する方法の例はたくさん見つかりましたが、ActualWidth.

4

2 に答える 2

9

必要なのは ReadOnly 依存関係プロパティです。ActualWidthProperty最初に行う必要があるのは、公開する必要があるコントロールへの依存関係の変更通知を利用することです。DependencyPropertyDescriptor次のように使用してそれを行うことができます。

// Need to tap into change notification of the FrameworkElement.ActualWidthProperty
Public MyUserControl()
{
   DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty
       (FrameworkElement.ActualWidthProperty, typeof(FrameworkElement));
   descriptor.AddValueChanged(this.MyElement, new EventHandler
            OnActualWidthChanged);
}

// Dependency Property Declaration
private static DependencyPropertyKey ElementActualWidthPropertyKey = 
      DependencyProperty.RegisterReadOnly("ElementActualWidth", typeof(double), 
      new PropertyMetadata());
public static DependencyProperty ElementActualWidthProperty = 
      ElementActualWidthPropertyKey.DependencyProperty;
public double ElementActualWidth
{
   get{return (double)GetValue(ElementActualWidthProperty); }
}
private void SetActualWidth(double value)
{
   SetValue(ElementActualWidthPropertyKey, value);
}

// Dependency Property Callback
// Called when this.MyElement.ActualWidth is changed
private void OnActualWidthChanged(object sender, Eventargs e)
{
   this.SetActualWidth(this.MyElement.ActualWidth);
}
于 2008-11-25T15:25:16.817 に答える
0

ActualWidthは読み取り専用のパブリック プロパティ ( から取得FrameworkElement) であり、既定で公開されます。あなたが達成しようとしているケースは何ですか?

于 2008-11-24T07:50:10.777 に答える