我有一个用户控件,它需要根据控件的总宽度设置它的一个变量。
<UserControl x:Class="MyControl"
...
x:Name="MyControlRoot">
<Grid DataContext="{Binding ElementName=MyControlRoot}">
<Canvas>
<Ellipse Width="{Binding MyWidth}" Height="{Binding MyHeight}"
在后面的代码中,属性定义如下:
public double MyWidth
{
get
{
return (ActualWidth - 5);
}
}
我尝试过ActualWidth
、Width
,甚至尝试访问根元素的宽度,但似乎无法在运行时确定控件的宽度;我要么得到0,要么得到NaN。
我的问题是:如何在运行时从控件中访问宽度?
发布于 2014-10-15 07:12:39
也许试试这个
Width="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ActualWidth}"
https://stackoverflow.com/questions/26376459
复制相似问题