【问题标题】:Using Interaction.Triggers in DataTemplate causing XamlParseException only in Designer在 DataTemplate 中使用 Interaction.Triggers 仅在设计器中导致 XamlParseException
【发布时间】:2020-07-16 03:49:49
【问题描述】:

我有一个带有对象绑定列表的 ItemsSource:


    <ItemsControl ItemsSource="{Binding Sprites}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Rectangle Fill="Black"
                                    Width="{Binding Width}"
                                    Height="{Binding Height}"/>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand Command="{Binding MouseDownOnSpriteCommand}"
                                                PassEventArgsToCommand="True"
                                                EventArgsConverter="{StaticResource EventToEventCommandConverter}"
                                                EventArgsConverterParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UIElement}, AncestorLevel=1}}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

现在您可以看到,我尝试为绑定的项目添加 DataTemplate,但这会导致 Visual Studio 设计器向我抛出以下错误消息:

XamlParseException: Collection property 'System.Windows.Controls.Grid'.'Triggers' is null.  
StackTrace:
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)  
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)  
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)  
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)  
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)  
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)  
at System.Windows.FrameworkElement.ApplyTemplate()  
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)  
at System.Windows.UIElement.Measure(Size availableSize)  
at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)  
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)  
at System.Windows.UIElement.Measure(Size availableSize)  
at System.Windows.ContextLayoutManager.UpdateLayout()  
at System.Windows.UIElement.UpdateLayout()  

然而,我的代码编译得非常好,我可以毫无问题地运行和执行它。问题是,只要取消注释 DataTemplate 代码,设计器只会向我显示错误消息。

我知道是 DataTemplate 导致了问题,因为当我将同一个 Grid 硬编码到 ItemsControl 中,然后使用 Interactions.Triggers 时,我可以绑定事件并且设计器中不会出现错误。 (但是我只能绑定到 MainViewModel 中的命令,而不是实际的项目列表)。

到目前为止,我已经尝试将模板移动到单独的 ResourceDictionary 中,但这并没有改变任何东西。 我也知道以前有人有同样的problem。但是我使用 Visual Studio 2019 并且我已经更新到最新版本,并且仍然出现错误。

【问题讨论】:

  • 不要期望设计器与运行时相提并论。此外,您的里程可能会因 Blend 而异。
  • 我不指望设计师能达到同等水平,我希望它不会出现故障并且至少可以工作,当我在做一件相对平常的事情时。此外,在另一个项目中,我也在 DataTemplate 中使用了 Interaction.Triggers,它的工作没有问题。我只是想不出为什么这段代码只为这个项目破坏了设计器

标签: wpf xaml xamlparseexception


【解决方案1】:

所以我还没有找到直接的解决方案,而是一种解决方法。 需要注意的一件事是,我使用

定义了我的 Window 的 DataContext
<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

而且我相信这会以某种方式影响错误。
经过测试,我发现如果我将 DataContext 的分配移动到代码后面,设计器不再显示错误并且工作正常。 所以基本上我用过

public MainWindow() {
    InitializeComponent();
    MainViewModel vm = new MainViewModel();
    DataContext = vm;
}

作为我后面代码的构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多