這是我簡化的場景。我可以使用鼠標調整我的內部矩形寬度。文本塊顯示隨着我調整它的寬度。我想要第二個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;
}
}