如何将视图模型中的数据绑定到用户控件资源中的对象?这是一个很抽象的例子:将资源绑定到视图模型
<UserControl ...
xmlns:local="clr-namespace:My.Local.Namespace"
Name="userControl">
<UserControl.Resources>
<local:GroupingProvider x:Key="groupingProvider" GroupValue="{Binding ???}" />
</UserControl.Resources>
<Grid>
<local:GroupingConsumer Name="groupingConsumer1" Provider={StaticResource groupingProvider"} />
<local:GroupingConsumer Name="groupingConsumer2" Provider={StaticResource groupingProvider"} />
</Grid>
</UserControl>
如何绑定GroupValue
在这个观点背后的视图模型的属性。我试过以下内容:
<local:GroupingProvider x:Key="groupingProvider" GroupValue="{Binding ElementName=userControl, Path=DataContext.Property}"/>
但是这不起作用。
编辑:
GroupProvider
延伸DependencyObject
和GroupValue
是DependencyProperty
的名称。我收到以下错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Property; DataItem=null; target element is 'GroupingProvider' (HashCode=47478197); target property is 'GroupValue' (type 'TimeSpan')
这似乎表明,它不能找到userControl
。
更多编辑:
任何人都没有回答我的问题吗?有没有办法做到这一点?
你有没有解决过这个问题?如果是的话,你会介意分享吗? – ZoolWay 2015-04-15 21:25:58