MJS*_*MJS 3 wpf binding user-controls readonly actualwidth
如何将ActualWidth用户控件的某个组件的属性公开给用户?
我已经找到了很多关于如何通过创建新的依赖项属性和绑定来公开普通属性的示例,但是没有关于如何公开只读属性的示例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);
}
Run Code Online (Sandbox Code Playgroud)