使用Interactivity wpf

时间:2018-02-21 10:01:57

标签: c# wpf interactive itemscontrol itemspaneltemplate

当我使用Interactivity时,我收到以下错误:

  

错误集合属性'System.Windows.Controls.Canvas'。'触发器'   是空的。

这是我的代码:

<ItemsControl x:Name="StreamCanvasItemSource" ItemsSource="{Binding StreamCanvasItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding ActualWidth, ElementName=StreamImage}" Height="{Binding ActualHeight, ElementName=StreamImage}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas x:Name="StreamCanvas" Background="Transparent" Width="{Binding ActualWidth, ElementName=StreamImage}" Height="{Binding ActualHeight, ElementName=StreamImage}" 
                AuxiliaryUtils:SizeObserver.Observe="True" AuxiliaryUtils:SizeObserver.ObservedWidth="{Binding CurrentCanvasWidth, Mode=OneWayToSource}" 
                AuxiliaryUtils:SizeObserver.ObservedHeight="{Binding CurrentCanvasHeight, Mode=OneWayToSource}" 
                AuxiliaryUtils:SizeObserver.ObservedLeft="{Binding CanvasLeft, Mode=OneWayToSource}" AuxiliaryUtils:SizeObserver.ObservedTop="{Binding CanvasTop, Mode=OneWayToSource}">
                <Interactivity:Interaction.Triggers>
                    <Interactivity:EventTrigger EventName="PreviewMouseWheel">
                        <Command:EventToCommand Command="{Binding OnMouseWheelCommand}" PassEventArgsToCommand="True" />
                    </Interactivity:EventTrigger>
                    <Interactivity:EventTrigger EventName="PreviewMouseDown">
                        <Interactivity:InvokeCommandAction Command="{Binding OnMouseDownCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}"/>
                    </Interactivity:EventTrigger>
                    <Interactivity:EventTrigger EventName="PreviewMouseMove">
                        <Interactivity:InvokeCommandAction Command="{Binding OnMouseMoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}"/>
                    </Interactivity:EventTrigger>
                    <Interactivity:EventTrigger EventName="PreviewMouseUp">
                        <Interactivity:InvokeCommandAction Command="{Binding OnMouseUpCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}}"/>
                    </Interactivity:EventTrigger>
                </Interactivity:Interaction.Triggers>
            </Canvas>
        </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

问题在于,当我运行程序时,一切都按预期工作,我在编译时看到错误,除非我评论Interactivity块,否则我看不到设计器。

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

datacontext如何连接到窗口/页面?建议将以下行添加到窗口/页面并将IsDesignTimeCreatable设置为true:

d:DataContext="{d:DesignInstance Type=yourviewmodetype, IsDesignTimeCreatable=True}"

通过这种方式,视图将绑定到设计时的viewmodel的值,这正是应用程序中出现问题的atm。