首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF UserControl公开ActualWidth

WPF UserControl公开ActualWidth
EN

Stack Overflow用户
提问于 2008-11-23 23:40:21
回答 2查看 3.7K关注 0票数 3

如何向用户公开我的用户控件的一个组件的ActualWidth属性?

我已经找到了许多关于如何通过创建新的依赖属性和绑定来公开普通属性的示例,但没有一个关于如何公开像ActualWidth这样的只读属性的示例。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-11-25 15:25:17

您需要的是一个ReadOnly依赖属性。您需要做的第一件事是利用您需要公开的控件上的ActualWidthProperty依赖项的更改通知。您可以像这样使用DependencyPropertyDescriptor来完成此操作:

代码语言:javascript
复制
// 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);
}
票数 9
EN

Stack Overflow用户

发布于 2008-11-24 07:50:11

ActualWidth是一个公共只读属性(来自FrameworkElement),默认情况下是公开的。您试图实现的情况是什么?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/313124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档