2011-03-24 107 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=.}" /> 

「路徑=」。將指向您的數據上下文。

相關問題