ActualWidth
ユーザー コントロールのコンポーネントの 1 つのプロパティをユーザーに公開するにはどうすればよいですか?
新しい依存関係プロパティとバインディングを作成して通常のプロパティを公開する方法の例はたくさん見つかりましたが、ActualWidth
.
ActualWidth
ユーザー コントロールのコンポーネントの 1 つのプロパティをユーザーに公開するにはどうすればよいですか?
新しい依存関係プロパティとバインディングを作成して通常のプロパティを公開する方法の例はたくさん見つかりましたが、ActualWidth
.
必要なのは 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);
}
ActualWidth
は読み取り専用のパブリック プロパティ ( から取得FrameworkElement
) であり、既定で公開されます。あなたが達成しようとしているケースは何ですか?