1

I've got a wpf view that has a telerik:RadListBox with a contextmenu associated on each item defined as

<DataTemplate x:Key="ListBoxItemTemplate">
        <Grid >
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu x:Name="ContextMenu" >
                    <!--cal:Action.TargetWithoutContext="{Binding}"-->
                    <!--TOREFACTOR cal:Message.Attach="Reconnect()" IsEnabled="{Binding IsAlive, Converter={StaticResource invertBooleanConverter}}"-->
                    <telerik:RadContextMenu.Items>
                        <telerik:RadMenuItem Header="{x:Static resources:CommonResources.LBL_RECONNECT}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <catel:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:RadListBox}},  Path=DataContext.Reconnect}" ></catel:EventToCommand>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            <telerik:RadMenuItem.IconTemplate>
                                <DataTemplate>
                                    <Image Source="/IF.Tesoreria.Client.WPF;component/Media/reconnect.png" Width="13" Height="13"/>
                                </DataTemplate>
                            </telerik:RadMenuItem.IconTemplate>
                        </telerik:RadMenuItem>
                    </telerik:RadContextMenu.Items>
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
            <Grid.ToolTip>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Text="Host:"></TextBlock>
                    <TextBlock Grid.Row="1" Text="Port:"></TextBlock>
                    <TextBlock Grid.Row="2" Text="Last Beat:"></TextBlock>

                    <TextBlock Grid.Row="0" Grid.Column="1" Margin="5,0,0,0" Text="{Binding Host}"></TextBlock>
                    <TextBlock Grid.Row="1" Grid.Column="1" Margin="5,0,0,0" Text="{Binding Port}"></TextBlock>
                    <TextBlock Grid.Row="2" Grid.Column="1" Margin="5,0,0,0" Text="{Binding LastHeartBeat, Converter={StaticResource dateTimeToStringConverter}, ConverterParameter=HH:mm:ss}"></TextBlock>
                </Grid>
            </Grid.ToolTip>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Image Source="{Binding IsAlive, Converter={StaticResource statusToBulletIconConverter}}" Margin="10 0 0 0" Width="16" Height="16" Grid.Column="0"
                    HorizontalAlignment="Left">
            </Image>
            <TextBlock Text="{Binding Description}" FontSize="12" FontFamily="Segoe UI" Grid.Column="1" Margin="10 0 0 0" HorizontalAlignment="Left" />
        </Grid>
    </DataTemplate>
<!--omiss-->    
<Grid>
    <telerik:RadListBox x:Name="listBox" ItemsSource="{Binding ServerList}" ItemTemplate="{StaticResource ListBoxItemTemplate}"/>
</Grid>

and in my viewmodel I've

  public ServerMonitorViewModel(IMonitorService moonitorService)
    {
        Reconnect = new Command<object>(OnReconnectExecute);
    }

    #region Commands
    public Command<object> Reconnect { get; private set; }
    #endregion

    private void OnReconnectExecute(object obj)
    {
        MessageBox.Show("ok");

    }

The error I got is

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadListBox', AncestorLevel='1''. BindingExpression:Path=DataContext.Reconnect; DataItem=null; target element is 'EventToCommand' (HashCode=46922521); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadListBox', AncestorLevel='1''. BindingExpression:Path=DataContext.Reconnect; DataItem=null; target element is 'EventToCommand' (HashCode=19649510); target property is 'Command' (type 'ICommand')

I've tried not to put a relativesource but it looks for the command inside my bounded item (the item bound to listitem)

How can I fix it up? Thanks

advapi
  • 3,661
  • 4
  • 38
  • 73
  • 1
    ContextMenu takes the DataContext of the ItemsControl and so it cannot access the ViewModel directly. Also It is not part of the VisualTree and so you cannot do RelativeSource binding. See my post http://stackoverflow.com/questions/30104193/how-to-bind-command-into-contextmenu-in-datatemplate/30105635#30105635 – Ayyappan Subramanian May 22 '15 at 14:25

1 Answers1

1

Light Switch beat me to the reason, use this instead.

Command="{Binding DataContext.Reconnect, ElementName=listBox}"

Here is a related question

Community
  • 1
  • 1
Mike Eason
  • 9,525
  • 2
  • 38
  • 63
  • I always get System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=listBox'. BindingExpression:Path=DataContext.Reconnect; DataItem=null; target element is 'EventToCommand' (HashCode=19341321); target property is 'Command' (type 'ICommand') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=listBox'. BindingExpression:Path=DataContext.Reconnect; DataItem=null; target element is 'EventToCommand' (HashCode=8260362); target property is 'Command' (type 'ICommand') – advapi May 22 '15 at 14:33