Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XAML中转发声明以解决DataTemplate的循环依赖关系_C#_Wpf_Xaml_Hierarchicaldatatemplate_Datatemplateselector - Fatal编程技术网

C# 在XAML中转发声明以解决DataTemplate的循环依赖关系

C# 在XAML中转发声明以解决DataTemplate的循环依赖关系,c#,wpf,xaml,hierarchicaldatatemplate,datatemplateselector,C#,Wpf,Xaml,Hierarchicaldatatemplate,Datatemplateselector,我想将HierarchycalDataTemplate与DataTemplateSelector一起使用,但顺序有问题: MyTemplateSelector依赖于TemplateA,TemplateA依赖于MyTemplateSelector。我在运行时收到System.Windows.Markup.XamlParseException。在XAML中是否有一种转发声明的方法,或者是否有不同的解决方案 解决方案: 我找到了解决方案:我不需要在TemplateA中设置TemplateSele

我想将HierarchycalDataTemplate与DataTemplateSelector一起使用,但顺序有问题:


MyTemplateSelector依赖于TemplateA,TemplateA依赖于MyTemplateSelector。我在运行时收到System.Windows.Markup.XamlParseException。在XAML中是否有一种转发声明的方法,或者是否有不同的解决方案

解决方案:
我找到了解决方案:我不需要在TemplateA中设置TemplateSelector,因为它是在父DataTemplate中设置的。有时它可能很简单…

[看起来您通过不加载资源解决了这个问题。我仍然会将答案发布给未来的读者]

您可以使用
DynamicResource
而不是
StaticResource


这只适用于填充依赖项属性,即
Binding。Converter=“{DynamicResource MyConverter}”
将不起作用,因为
Binding。Converter
不是依赖项属性。

[您似乎通过不加载资源解决了这个问题。我仍然会将答案发布给未来的读者]

您可以使用
DynamicResource
而不是
StaticResource


这仅适用于填充依赖项属性的情况,即
Binding.Converter=“{DynamicResource MyConverter}”
将不起作用,因为
Binding.Converter
不是依赖项属性。

必须有更好的方法来执行您尝试执行的操作。。。你能分享一下你在哪里应用你的模板,你想绑定什么样的数据结构吗?我已经添加了更多的信息,但我认为它们没有帮助。要绑定的数据结构非常复杂,在这里用一种简单的方式显示它,但我知道:我需要DataTemplateSelector。一定有更好的方法来完成您正在尝试的操作。。。你能分享一下你在哪里应用你的模板,你想绑定什么样的数据结构吗?我已经添加了更多的信息,但我认为它们没有帮助。要绑定的数据结构非常复杂,在这里以简单的方式显示它,但我知道:我需要DataTemplateSelector。
<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="TemplateA"
                              ItemsSource="{Binding AnySource}"
                              ItemTemplateSelector="{StaticResource MyTemplateSelector}" >
        <Label Content="A" />
    </HierarchicalDataTemplate>

    <DataTemplate x:Key="TemplateB">
        <Label Content="B" />
    </DataTemplate>

    <viewmodel:MyTemplateSelector 
        TemplateA="{StaticResource TemplateA}" 
        TemplateB="{StaticResource TemplateB}" 
        x:Key="MyTemplateSelector" />

    <HierarchicalDataTemplate x:Key="TemplateC"
                              ItemsSource="{Binding AnotherSource}"
                              ItemTemplateSelector="{StaticResource MyTemplateSelector}">
        <Label Content="C" />
    </HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
    <TreeView 
        ItemsSource="{Binding Source={StaticResource SomeList}}"
        ItemTemplate="{StaticResource TemplateC}"/>
</Grid>