2014-10-12 45 views
-1

我想这取决于AC#代码。我的变量的矩形的高度属性绑定在XML如何将高度属性绑定到一个变量在C#代码WPF

<Rectangle x:Name="H1" Fill="Blue" HorizontalAlignment="Left" Height="{Binding Path=H1 }" Margin="29,83,0,0" Stroke="Black" VerticalAlignment="Top" Width="149"/> 

写这段代码并且在窗口加载事件的C#代码:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    int H1 = 50; 
} 

但没有行动,高度为0,intelicense对我说,“H1”从未使用过 ,问题出在哪里?

+1

你在代码中有很多问题。您应该调查[数据绑定概述](http://msdn.microsoft.com/zh-cn/library/ms752347(v = vs.110).aspx)文档。 – 2014-10-12 09:55:51

回答

0

您需要使用以下代码来设置高度。

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    this.H1.DataContext = new { H1 = 50 }; // here this.H1, refers to <Rectangle x:Name="H" .. 
}