【发布时间】:2010-12-23 14:14:35
【问题描述】:
这是我的 XAML:
<Window x:Class="Gui.Wpf.MoveElementWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:Gui.Wpf.Converters"
Title="Move the red element" Height="300" Width="500">
<Window.Resources>
<!-- Converter for element location -->
<converters:LocationConverter x:Key="LocationConverter" />
</Window.Resources>
<Grid>
<Rectangle
Name="RedRectangle"
Width="150"
Height="80"
Stroke="Black"
Fill="Red"
MouseDown="RedRectangle_MouseDown"
MouseMove="RedRectangle_MouseMove" />
<TextBlock
Name="StatusTextBlock"
HorizontalAlignment="Left"
VerticalAlignment="Bottom" />
</Grid>
</Window>
我希望能够将屏幕上RedRectangle 的位置数据绑定到StatusTextBlock 的Text 属性,即我希望StatusTextBlock 说:“红色矩形的位置是:12、18 “ 例如。
我为Point 到String 的转换创建了一个转换器:
[ValueConversion(typeof(Point), typeof(String))]
public class LocationConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
Point location;
string valueString;
location = (Point)value;
valueString = string.Format("Red rectangle's location is: {0}, {1}",
location.X, location.Y);
return valueString;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
我不知道如何对矩形的位置进行数据绑定,因为它不是通过属性提供的,而是通过方法提供的:RedRectangle.PointToScreen(new Point(0,0));。请帮忙,谢谢。
【问题讨论】:
标签: wpf data-binding binding