Невозможно правильно связать команду с viewmodel

У меня есть представление wpf, у которого есть telerik:RadListBox с контекстным меню, связанным с каждым элементом, определенным как

<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>

и в моей модели представления я

  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");

    }

Я получил ошибку

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')

Я пытался не помещать родственник, но он ищет команду внутри моего ограниченного элемента (элемент, связанный с listitem)

Как я могу это исправить? Спасибо

1 ответ

Переключатель света побил меня до основания, используйте это вместо этого.

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

Вот связанный вопрос

Другие вопросы по тегам