2011-03-24 81 views
0

这是我简化的场景。我可以使用鼠标调整我的内部矩形宽度。文本块显示随着我调整它的宽度。我想要第二个textblock显示一个属性的值也随宽度而变化,但我无法弄清楚如何绑定它。我如何绑定到一个属性?

<Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Center"> 

    <Rectangle x:Name="aRec" Height="100" Width="100" MinWidth="10" Fill="Blue" /> 

    <Rectangle x:Name="myRec" Height="100" Width="300" MinWidth="10" Fill="Red" Opacity="0.5" 
       MouseLeftButtonDown="myRec_MouseLeftButtonDown" 
       MouseLeftButtonUp="myRec_MouseLeftButtonUp" 
       MouseMove="myRec_MouseMove"></Rectangle> 

    <StackPanel> 
     <TextBlock x:Name="myText1" Width="40" Height="20" Foreground="White" Text="{Binding ElementName=aRec, Path=Width}" /> 
     <TextBlock x:Name="myText2" Width="40" Height="20" Foreground="White" Text="{Binding Value}" /> 
    </StackPanel> 

</Grid> 

public partial class MainPage : UserControl 
{ 
    Boolean active = false; 

    private Double _value; 
    public Double Value 
    { 
     get { return _value; } 
     set { _value = value; } 
    } 

    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void myRec_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     active = true; 
    } 

    private void myRec_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (active == true) 
     { 
      aRec.Width = e.GetPosition(myRec).X; 
      _value = aRec.Width * 10; 
     } 
    } 

    private void myRec_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     active = false; 
    }   
} 

回答

0

您必须声明属性“Value”作为依赖项属性。

0

在你的后台代码:

myText2.DataContext = Value; 

在您的XAML:

<TextBlock x:Name="myText2" Width="40" Height="20" Foreground="White" Text="{Binding Path=.}" /> 

“路径=”。将指向您的数据上下文。

相关问题