我正在开发一个派生自 UserControl
的图表控件,一切都很好,直到我发现一个 DependencyProperty
不能通过绑定(bind)设置底轴。这是图表控件的代码片段:
public class AxisBase : FrameworkElement
{
public IList<double> ExtraGridLines
{
get { return (IList<double>)GetValue(ExtraGridLinesProperty); }
set { SetValue(ExtraGridLinesProperty, value); }
}
public static readonly DependencyProperty ExtraGridLinesProperty =
DependencyProperty.Register("ExtraGridLines", typeof(IList<double>), typeof(AxisBase), new PropertyMetadata(null, OnExtraGridLineDataPropertyChanged));
private static void OnExtraGridLineDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Problem: data bing would not work, and
//this call back method will never get called!
Debug.WriteLine("ExtraGridLines changed...");
}
}
public sealed partial class MyChart : UserControl
{
public MyChart()
{
this.InitializeComponent();
}
public AxisBase BottomAxis
{
get { return (AxisBase)GetValue(BottomAxisProperty); }
set { SetValue(BottomAxisProperty, value); }
}
public static readonly DependencyProperty BottomAxisProperty =
DependencyProperty.Register("BottomAxis", typeof(AxisBase), typeof(MyChart), new PropertyMetadata(null));
}
这是绑定(bind)代码:
<Page x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
x:Name="rootPage">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:MyChart>
<local:MyChart.BottomAxis>
<local:AxisBase ExtraGridLines="{Binding MyExtraGridLines, ElementName=rootPage}" />
</local:MyChart.BottomAxis>
</local:MyChart>
</Grid>
</Page>
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
private List<double> _myExtraGridLines;
public List<double> MyExtraGridLines
{
get { return _myExtraGridLines; }
set
{
_myExtraGridLines = value;
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyExtraGridLines"));
}
}
public MainPage()
{
this.InitializeComponent();
this.MyExtraGridLines = new List<double>() { 10, 100, 1000 };
}
public event PropertyChangedEventHandler PropertyChanged;
}
问题是:绑定(bind)似乎从未起作用,dp 的 PropertyChangedCallback 方法 ExtraGridLines
永远不会被调用。但是这些代码在 WPF 中有效。我的代码有什么问题?或者这是一个错误?
任何想法将不胜感激!
编辑1:
这似乎与数据类型无关,即使我已经将类型从 IList<double>
更改为至 int
, string
或 object
,问题是一样的。但是如果我改变 AxisBase
的类型来自 FrameworkElement
至 DependencyObject
,那么绑定(bind)就会起作用。但是这个解决方案是 Not Acceptable ,因为DependencyObject
没有Style
.
编辑2: 最后,我想我找到了 ElementName 绑定(bind)不起作用的原因:ElementName 绑定(bind)仅适用于同一 NameScope 中的元素。如果您有兴趣了解更多信息,请查看这两个很好的链接: https://stackoverflow.com/questions/18389118/how-does-binding-elementname-work-exactly和 http://www.cnblogs.com/idior/archive/2010/05/28/1746513.html
顺便说一句:一开始我错了:这些代码在 WPF 中也不起作用。绑定(bind)会抛出一个运行时错误: System.Windows.Data 错误:4:找不到引用“ElementName=rootPage”的绑定(bind)源。绑定(bind)表达式:Path=ShowAxisLabel;数据项=空;目标元素是“AxisBase”(名称='');目标属性是“IsLabelVisible”(类型“ bool 值”)
顺便说一句 2:要使 DataContext 绑定(bind)在上面的代码中起作用,AxisBase
应该监听 MyChart
的 DataContext 的变化, 或显式添加到 MyChart 的逻辑树或可视树中。那是我的代码的一个错误。但是由于 NameScope 问题,似乎没有正常或优雅的方法来使 ElementName 在这里绑定(bind)。
也许我应该将这个问题的标题更改为:为什么 ElementName 绑定(bind)到 UserControl 中的元素在 UWP/WPF 中不起作用?
最佳答案
这可能是 UWP 中传统绑定(bind)的错误;然而,有了新的 x:Bind
,下面的代码应该按预期工作。
<local:AxisBase ExtraGridLines="{x:Bind MyExtraGridLines, Mode=OneWay}" />
请注意,这也会为您带来更好的性能。因此,您应该始终首先考虑使用这种绑定(bind)方法。
更新
看起来真正的问题与 ElementName
有关绑定(bind)到一个祖先。但是,如果您通过指定 DataContext
使用正常的 MVVM 方法像这样 -
MyExtraGridLines = new List<double>() { 10, 100, 1000 };
DataContext = this;
并删除 ElementName
与普通的绑定(bind)-
<local:AxisBase ExtraGridLines="{Binding MyExtraGridLines}" />
还要确保轴元素在可视化树中,方法是添加 -
<UserControl x:Class="xxx.MyChart">
<Grid>
<ContentPresenter Content="{x:Bind BottomAxis, Mode=OneWay}" />
</Grid>
</UserControl>
这也应该有效。
关于c# - UWP 错误?绑定(bind)到某些嵌套的 DependencyProperty 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612644/