建立在C#WPF中的绑定点 [英] Creating a bindable Point in C# WPF
问题描述
我知道多重继承已经出来了,但有一个方法来创建一个System.Windows.Point包装,可以从它继承,但仍实现绑定依赖属性?
I know multiple inheritence is out, but is there a way to create a wrapper for System.Windows.Point that can inherit from it but still implement bindable dependency properties?
我试图代码,以便为我的XAML我可以像没有问题,下面创建staments:
I'm trying to code so that for my XAML I can create staments like the following without issue:
<定制:X点={结合宽度,的ElementName = ParentControlName}Y ={结合的高度,的ElementName = ParentControlName}/>
这会使编码之类的多边形,路径,LineSegments和其他控制容易得多。
It would make coding things like Polygons, Paths, LineSegments and other controls so much easier.
下面的代码是作为一厢情愿,我明白,这绝不会的工作,但是这是我希望能够做的事情的那种:
The following code is provided as wishful thinking and I understand that it will in no way work, but this is the kind of thing I want to be able to do:
public class BindablePoint: DependencyObject, Point
{
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(BindablePoint),
new FrameworkPropertyMetadata(default(double), (sender, e) =>
{
BindablePoint point = sender as BindablePoint;
point.X = (double) e.NewValue;
}));
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(double), typeof(BindablePoint),
new FrameworkPropertyMetadata(default(double), (sender, e) =>
{
BindablePoint point = sender as BindablePoint;
point.Y = (double)e.NewValue;
}));
public new double X
{
get { return (double)GetValue(XProperty); }
set
{
SetValue(XProperty, value);
base.X = value;
}
}
public new double Y
{
get { return (double)GetValue(YProperty); }
set
{
SetValue(YProperty, value);
base.Y = value;
}
}
}
推荐答案
不幸的是,点
是一个结构,而结构不要科技支撑继承。从 MSDN
Unfortunately, Point
is a struct, and structs don't support inheritance. From MSDN
注意的结构不支持
继承,但他们可以实现
接口。欲了解更多信息,请参阅
接口(C#编程指南)
也许这并不直接回答你的问题,但你可以很容易地绑定一个点
用转换器的帮助。在你的情况下,它会像
Maybe this doesn't answer your question directly but you can easily bind a Point
with the help of a Converter. In your case it would be like
<SomeControl.SomePointProperty>
<MultiBinding Converter="{StaticResource PointConverter}">
<Binding ElementName="ParentControlName"
Path="Width"/>
<Binding ElementName="ParentControlName"
Path="Height"/>
</MultiBinding>
</SomeControl.SomePointProperty>
PointConverter
public class PointConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double xValue = (double)values[0];
double yValue = (double)values[1];
return new Point(xValue, yValue);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
如果你只是想在X值绑定,并有静态Y值,你可以做到这一点像
If you just want to bind the X value and have a static Y value you could do this like
<SomeControl SomePointProperty="{Binding Path=Width,
ElementName=ParentControlName,
Converter={StaticResource PointXConverter},
ConverterParameter=20}"/>
PointXConverter
public class PointXConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double progressBarValue = (double)value;
double yValue = System.Convert.ToDouble(parameter);
return new Point(progressBarValue, yValue);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于建立在C#WPF中的绑定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!