WPF data binding template event does not trigger problems

  Today, more leisure, a practiced hand to do the project, the results of a user to write a data template in xaml after, on a Canvas which is bundled with a left click of the mouse events, results when debugging, no matter how clicks are not jump to breakpoint there, baffled.

After trying not binding events, the direct use of the wording in the joint event, the event will be written on the controls and handling events in cs code instead of writing ViewModel, you can use, so you can judge when using MVVM, the command did not succeed bound to the event.

At first I thought was a BUG MVVM framework DevExpress. Command later changed his own handwriting MVVM, does not solve the problem, think again, think it is not a data template to get DataContext lead, then Canvas will be bound to make the following changes to the data context Canvas is clearly uppermost parent node the data context, the revised solve the problem, on reflection think it is a form of resources will not be modified by the context of the form, personal opinion, not verification .

before fixing:

<Canvas Width="350" Height="80">
                    <dxmvvm:Interaction.Triggers>
                        <dxmvvm:EventToCommand EventName="MouseLeftButtonDown"  
                                               Command="{Binding ClickChatWindowToSomeOne}"  
                                               PassEventArgsToCommand="True"/>
                    </dxmvvm:Interaction.Triggers>
                    <Image Width="60" Height="60" Canvas.Top="30" Canvas.Left="20">
                        <Image.Source>
                            <MultiBinding Converter="{StaticResource imageconveter}">
                                <Binding Path="headimg"/>
                                <Binding Path="sex"/>
                            </MultiBinding>
                        </Image.Source>
                    </Image>
                    <Canvas Background="LightPink">
                        <TextBlock Text="{Binding uname}" Canvas.Left="100" Canvas.Top="40" FontSize="15" Foreground="Black" FontWeight="Bold">
                        </TextBlock>
                        <TextBlock Text="{Binding describe}"  Canvas.Left="100" Canvas.Top="70" FontSize="10" Foreground="Black"/>
                        <StackPanel Orientation="Horizontal" Canvas.Top="40" Canvas.Left="220">
                            <Image Source="image/location.png" Width="40" Height="40"/>
                            <TextBlock Text="{Binding lastlocal}" FontSize="10" FontWeight="Bold" VerticalAlignment="Center"></TextBlock>
                        </StackPanel>
                    </Canvas>
                    <Separator Margin="0, 5, 0, 5"/>
                </Canvas>

Modified: {modified AncestorType is to call the parent node of the data template }

<Canvas Width="350" Height="80">
                    <dxmvvm:Interaction.Triggers>
                        <dxmvvm:EventToCommand EventName="MouseLeftButtonDown"  
                                               Command="{Binding DataContext.ClickChatWindowToSomeOne,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"  
                                               PassEventArgsToCommand="True"/>
                    </dxmvvm:Interaction.Triggers>
                    <Image Width="60" Height="60" Canvas.Top="30" Canvas.Left="20">
                        <Image.Source>
                            <MultiBinding Converter="{StaticResource imageconveter}">
                                <Binding Path="headimg"/>
                                <Binding Path="sex"/>
                            </MultiBinding>
                        </Image.Source>
                    </Image>
                    <Canvas Background="LightPink">
                        <TextBlock Text="{Binding uname}" Canvas.Left="100" Canvas.Top="40" FontSize="15" Foreground="Black" FontWeight="Bold">
                        </TextBlock>
                        <TextBlock Text="{Binding describe}"  Canvas.Left="100" Canvas.Top="70" FontSize="10" Foreground="Black"/>
                        <StackPanel Orientation="Horizontal" Canvas.Top="40" Canvas.Left="220">
                            <Image Source="image/location.png" Width="40" Height="40"/>
                            <TextBlock Text="{Binding lastlocal}" FontSize="10" FontWeight="Bold" VerticalAlignment="Center"></TextBlock>
                        </StackPanel>
                    </Canvas>
                    <Separator Margin="0, 5, 0, 5"/>
                </Canvas>

 

  

Guess you like

Origin www.cnblogs.com/qwqwQAQ/p/11569787.html