我正在努力绑定到依赖属性。
我的应用程序有一个MainWindow。这是一个usercontrol(称为时间轴)。在TimeLine UserControl中是另一个名为MoveMe的Control。
我可以从MainWindow绑定到时间轴UserControl的依赖项属性。当我使用OneWayToSource
绑定时,我可以从MoveMe绑定到MoveMe UserControl。但是,我正在尝试从时间轴UserControl绑定到MoveMe控件(从父级到子级)。但是,绑定不起作用。我把一只手表放在MoveMe财产的制定者身上,它从未被解雇过。
“输出”窗口中没有绑定问题。
我的MainWindow.xaml已
<timeline:TimeLine StartTime="{Binding StartTime}"
EndTime="{Binding EndTime}"
TestEvents="{Binding TestEvents}"
CampaignDetails="{Binding CampaignDetails}"
ShowReportPeriod="True" HorizontalAlignment="Stretch"/>
我的时间线.xaml
<userControls:MoveMe
StartMarkerPositionPixels="{Binding RelativeSource={RelativeSource AncestorType=userControls:TimeLine}, Path=StartMarkerPosition, Mode=OneWayToSource}"
EndReportPositionInPixels="{Binding RelativeSource={RelativeSource AncestorType=userControls:TimeLine}, Path=EndOfReportPosition, Mode=OneWay}"
x:Name="ReportSelectorStart" />
并且它的DataContext设置为
<UserControl DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=Window}}"...
因此,正如您在上面所见,在timeline.xaml中有2个属性被绑定,第一个是StartMarkerPositionPixels,它是OneWayToSource。这很好用。问题是第二个,EndReportPositionInPixels,因为它没有绑定。
MoveMe控件背后的代码
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace TimeLineCanvas.UserControls
{
/// <summary>
/// Interaction logic for MoveMe.xaml
/// </summary>
public partial class MoveMe : UserControl
{
public MoveMe()
{
InitializeComponent();
//this.DataContext = this;
}
public double StartMarkerPositionPixels
{
get { return (double)GetValue(StartMarkerPositionProperty); }
set { SetValue(StartMarkerPositionProperty, value); }
}
public double EndReportPositionInPixels
{
get { return (double)GetValue(ScaleFactorProperty); }
set { SetValue(ScaleFactorProperty, value);
OnPropertyChanged("EndReportPositionInPixels");
}
}
public static readonly DependencyProperty EndMarkerPositionProperty = DependencyProperty.Register(
"EndMarkerPositionPixels",
typeof(double),
typeof(MoveMe));
public static readonly DependencyProperty EndReportPositionInPixelsPoperty = DependencyProperty.Register(
"EndReportPositionInPixels",
typeof(double),
typeof(MoveMe));
}
}
在我的TimeLine代码隐藏中,我有以下
private double _endOfReportPosition;
public double EndOfReportPosition
{
get { return _endOfReportPosition; }
set
{
_endOfReportPosition = value;
//this.ReportSelectorStart.EndMarkerPositionPixels = value;//If I hardcode it in, then it works, but I want it bind
OnPropertyChanged("EndOfReportPosition");
}
}
输出窗口确认没有绑定错误。
我的问题是,如何通过依赖项属性将TimeLine控件中的值传递给我的MoveMe控件。
答案 0 :(得分:2)
首先,您不需要INPC用于依赖项属性。再看看这段代码 -
public double EndReportPositionInPixels
{
get { return (double)GetValue(ScaleFactorProperty); }
set { SetValue(ScaleFactorProperty, value); }
}
您在此处设置并获取ScaleFactorProperty
的值。不应该EndReportPositionInPixelsPoperty
这样 -
public double EndReportPositionInPixels
{
get { return (double)GetValue(EndReportPositionInPixelsPoperty); }
set { SetValue(EndReportPositionInPixelsPoperty, value); }
}