【发布时间】:2018-09-03 20:12:28
【问题描述】:
我在 .NET 中相对较新,所以,很可能我在做一些愚蠢的事情,但我所做的只是 我用 XAML 创建了一个自定义形状,它基本上是一个圆形。
Circle.xaml:
<local:Closed2DArea
x:Class="GeoDraw.CustomShapes.Circle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GeoDraw.CustomShapes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Name="circle">
<Path.Data>
<EllipseGeometry Center="{Binding Center, ElementName=circle}" RadiusX="{Binding Radius, ElementName=circle}" RadiusY="{Binding Radius, ElementName=circle}"/>
</Path.Data>
</local:Closed2DArea>
现在,Circle.xaml.cs: 文件有两个 DependencyProperty :CenterProperty 和 RadiusProperty
public static readonly DependencyProperty CenterProperty = DependencyProperty.Register("Center", typeof(Point), typeof(Circle), null);
public Point Center
{
get => (Point)GetValue(CenterProperty);
set => SetProperty(CenterProperty, value);
}
public static readonly DependencyProperty RadiusProperty = DependencyProperty.Register("Radius", typeof(double), typeof(Circle), null);
public double Radius
{
get => (double)GetValue(RadiusProperty);
set => SetProperty(RadiusProperty, value);
}
这里的SetProperty 方法是在基类Closed2DArea 中声明的,它实现了INotifyPropertyChanged,如下所示:
public abstract class Closed2DArea : Path , INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void SetProperty<T>( DependencyProperty prop, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
SetValue(prop, value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/*constructors and other methods*/
}
当我在具有正常属性设置的视图中使用此形状时,所有这些都可以正常工作,例如 <CustomShape:Circle Center="20 20" Radius="10">。
问题::
当我尝试像这样使用数据绑定时会出现问题:
<Grid x:Name="maingrid" >
<TextBox x:Name="tbox" Text="80" Margin="258,61,82,189" Width="100" Height="30"/>
<CustomShape:Circle Center="100,80" Radius="{Binding Text, ElementName=tbox}"/>
</Grid>
这里的绑定不起作用,圆永远不会改变它的半径。我坚持了 5 个小时,尝试了很多,但不知道我错过了什么。有什么帮助吗?
【问题讨论】:
标签: .net xaml data-binding uwp uwp-xaml