在 wpf 中如何访问 usercontrol 中的 window.resources

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15419646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:05:14  来源:igfitidea点击:

In wpf how to access window.resources in usercontrol

wpfuser-controlsresourcescollectionviewsource

提问by Ash

I have a child usercontrol and I am having trouble accessing its parent usercontrol's resources. The resource is a collectionViewSource and its source is set in the code behind of the parent usercontrol, the source is set to a query result from LINQ to SQL. Both usercontrols are defined in their own xaml file. I tried using DynamicResource and it complained saying it needs to be used with a DepenednecyProperty or so...

我有一个子用户控件,但无法访问其父用户控件的资源。资源是一个collectionViewSource,它的来源设置在父用户控件后面的代码中,来源设置为LINQ to SQL的查询结果。两个用户控件都在它们自己的 xaml 文件中定义。我尝试使用 DynamicResource 并且它抱怨说它需要与 DepenednecyProperty 一起使用......

parent usercontrol:

父用户控件

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

child usercontrol:

子用户控件

...
<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"/>
...

The error I am getting is 'The resource "UsageTypeSource" could not be resolved'

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

Can you provide the proper way to access the resource

您能否提供访问资源的正确方法

Thanks in advance Ash

提前致谢

采纳答案by Federico Berasategui

Try this:

尝试这个:

ItemsSource="{DynamicResource UsageTypeSource}}"/>