2013-03-14 223 views
2

我有一个孩子usercontrol,我无法访问其父级usercontrol的资源。资源是一个collectionViewSource,它的源代码在父代usercontrol后面的代码中设置,源代码被设置为从LINQ到SQL的查询结果。两个用户控件均在其自己的xaml文件中定义。我尝试使用DynamicResource,它抱怨说,它需要与DepenednecyProperty左右被用来...在wpf如何访问usercontrol中的window.resources

父用户控件

... 
<UserControl.Resources> 
    <CollectionViewSource x:Key="UsageTypeSource"/> 
</UserControl.Resources> 
... 

子用户控件

... 
<ComboBox Height="28" HorizontalAlignment="Left" Margin="147,1,0,0" x:Name="cbxUsage" 
      Grid.Column="1" Grid.Row="2" Width="186" 
      SelectedItem="{Binding Path=UsageType, ValidatesOnExceptions=true, NotifyOnValidationError=true}" 
      VerticalAlignment="Top" SelectedValuePath="ID" 
      SelectedValue="{Binding Path=Usage}" 
      ItemsSource="{Binding Source={StaticResource UsageTypeSource}}" 
      DisplayMemberPath="Type"/> 
... 

的错误我得到的是'资源'UsageTypeSource“无法解析'

你能否提供适当的方式来访问资源

在此先感谢 灰

+1

它不相关的问题,但它真的有意义地结合' SelectedItem'和'SelectedValue'? – Clemens 2013-03-14 20:43:44

+0

@Clemens它在某些情况下,我不知道具体这一个。 – 2013-03-14 20:47:26

+0

我最初使用SelectedValue并忘记删除它,谢谢指出它。 – Ash 2013-04-11 15:39:10

回答

2

试试这个:

ItemsSource="{DynamicResource UsageTypeSource}}"/> 
+0

@Clemens不是真的,他可能在做'{Binding Source = {DynamicResource ..}}'这是不允许的,因为'Binding'类的Source属性不是DP。 – 2013-03-14 20:42:25

+0

+1,你说得对。删除了我的评论。 – Clemens 2013-03-14 20:45:41

+0

谢谢你的帮助... – Ash 2013-03-14 21:01:40