xst*_*ter 6 data-binding xaml windows-runtime winrt-xaml
我试图让2个控件具有相同的高度.我可以只使用XAML吗?
如果我做了类似的事情,<Canvas Height="{Binding Height, ElementName=AnotherControl}" />它实际上并没有做任何事情,并且高度变为零.输出面板不会抱怨任何绑定错误,因此AnotherControl.Height确实存在.我尝试绑定到ActualHeight,但它也没有做任何事情.
我错过了什么?
K M*_*hta 11
我的猜测是你AnotherControl没有明确给出一个Height.不幸的是,在WinRT中(与WPF不同,但与Silverlight相同),ActualWidth并且ActualHeight被称为"计算属性".这意味着属性更改事件在更改时不会在内部引发.因此,绑定它们并不可靠,正如您所注意到的那样,它不会起作用.
旁注:它可能会不时地工作,但这纯粹是因为绑定框架所产生的get调用的时间ActualHeight.
就目前而言,您不能仅使用XAML.您必须ActualControl.SizeChanged在代码隐藏中处理事件,并将其设置Height为AnotherControl.ActualHeight显式.
正如Kshitij Mehta所提到的,在WinRT中绑定到ActualHeight和ActualWidth是不可靠的.但是有一个很好的解决方法,你不必使用SizeChanged-Event:
添加此课程:
public class ActualSizePropertyProxy : FrameworkElement, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public FrameworkElement Element
{
get { return (FrameworkElement)GetValue(ElementProperty); }
set { SetValue(ElementProperty, value); }
}
public double ActualHeightValue
{
get { return Element == null ? 0 : Element.ActualHeight; }
}
public double ActualWidthValue
{
get { return Element == null ? 0 : Element.ActualWidth; }
}
public static readonly DependencyProperty ElementProperty =
DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(ActualSizePropertyProxy),
new PropertyMetadata(null, OnElementPropertyChanged));
private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ActualSizePropertyProxy)d).OnElementChanged(e);
}
private void OnElementChanged(DependencyPropertyChangedEventArgs e)
{
FrameworkElement oldElement = (FrameworkElement)e.OldValue;
FrameworkElement newElement = (FrameworkElement)e.NewValue;
newElement.SizeChanged += new SizeChangedEventHandler(Element_SizeChanged);
if (oldElement != null)
{
oldElement.SizeChanged -= new SizeChangedEventHandler(Element_SizeChanged);
}
NotifyPropChange();
}
private void Element_SizeChanged(object sender, SizeChangedEventArgs e)
{
NotifyPropChange();
}
private void NotifyPropChange()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ActualWidthValue"));
PropertyChanged(this, new PropertyChangedEventArgs("ActualHeightValue"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
将它放在资源中:
<UserControl.Resources>
<c:ActualSizePropertyProxy Element="{Binding ElementName=YourElement}" x:Name="proxy" />
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
并绑定到它的属性:
<TextBlock x:Name="tb1" Text="{Binding ActualWidthValue, ElementName=proxy}" />
Run Code Online (Sandbox Code Playgroud)