user29782711
user29782711

Reputation: 7

Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''

PLEASE HELP!! I have the following code in WPF Model is class ImageModel.cs

<ItemsControl ItemsSource="{Binding Bitmaps}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                ...
                    </Grid>
                </Grid>
                
                <!--<Grid Grid.Column="1" Margin="0 5">
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" Command="{Binding Path=DataContext.MoveUpCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}">/\</Button>
                    <Button Grid.Row="1" Command="{Binding Path=DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}">X</Button>
                    <Button Grid.Row="2" Command="{Binding Path=DataContext.MoveDownCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}">\/</Button>
                </Grid>-->
                <Grid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Up" Command="{Binding Path=DataContext.MoveUpCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}"></MenuItem>
                        <MenuItem Header="Down" Command="{Binding Path=DataContext.MoveDownCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding}"></MenuItem>
                    </ContextMenu>
                </Grid.ContextMenu>
                
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Commented section of code is working perfectly. But if I change this to ContextMenu I'll get Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''.

I tried to change AncestorType to ContextMenu or Menu item. I got error message about 'MoveUpCommand' property not found on 'object' ''ImageModel' But command MoveUpCommand is located in other file. When I used commented section it worked. What I need to change

Upvotes: -2

Views: 29

Answers (0)

Related Questions