【发布时间】:2010-12-31 07:02:58
【问题描述】:
显示一个矩形并将 Width、Height、Angle 绑定到视图模型类,正如我在 XAML 中所期望的那样
<Rectangle
RenderTransformOrigin="0.5,0.5"
Fill="Black"
Width="{Binding Path=Width, Mode=TwoWay}"
Height="{Binding Path=Height, Mode=TwoWay}">
<Rectangle.RenderTransform>
<RotateTransform Angle="{Binding Path=Angle, Mode=TwoWay}" />
</Rectangle.RenderTransform>
</Rectangle>
但是,在代码隐藏中创建矩形时,我可以绑定到高度和宽度,但不能绑定到角度。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Binding bindH = new Binding("Height");
bindH.Mode = BindingMode.TwoWay;
Binding bindW = new Binding("Width");
bindW.Mode = BindingMode.TwoWay;
// DOES NOT WORK
// AND I DID TRY MANY OTHER COMBINATIONS
Binding bindA = new Binding("Angle");
bindA.Mode = BindingMode.TwoWay;
Rectangle r1 = new Rectangle();
SolidColorBrush myBrush = new SolidColorBrush(Colors.Black);
r1.Fill = myBrush;
r1.RenderTransformOrigin = new Point(0.5,0.5);
r1.SetBinding(Rectangle.WidthProperty, bindW);
r1.SetBinding(Rectangle.HeightProperty, bindH);
** // 不工作**
r1.SetBinding(RenderTransformProperty, bindA);
LayoutPanel.Children.Add(r1); // my custom layout panel
}
感谢所有帮助。
【问题讨论】:
标签: wpf xaml binding code-behind