简体   繁体   中英

Accessing another data context in custom control

Hello Stackoverflowers,

In a .xaml file, i have set a datacontext to the associated ViewModel.

  d:DataContext="{d:DesignInstance Contents:OverlayDesignerViewModel}">

Later in the file, i would like to use a class that inherit from System.Windows.Controls.Canvas.

I would like to set Interaction.Triggers in this control but I want the Command to be in the DataContext defined at the beggining of the XAML file.

    <Contents:DrawingCanvasCustom 
                Background="#00000000"
                Name="_drawingCanvas"
                Width="650"
                Height="450">

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="CanvasMouseDown">
                <i:InvokeCommandAction Command="{Binding Path=DataContext.CanvasMouseDownCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                   CommandParameter="{Binding ElementName=CanvasOverlayView}"/>
            </i:EventTrigger>

However i have an error : "cannot rsolve property CanvasMouseDownCommand in data context of type object".

How to solve this ?

您在这里不见了Mode=FindAncestor

Command="{Binding Path=DataContext.CanvasMouseDownCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM