如何將視圖模型中的數據綁定到用戶控件資源中的對象?這是一個很抽象的例子:將資源綁定到視圖模型
<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