无法将命令正确绑定到viewmodel

时间:2015-05-22 14:18:36

标签: wpf catel

我有一个wpf视图,它有一个telerik:RadListBox,每个项目定义的上下文菜单定义为

rm(list = ls())
require(reshape)
require(rCharts)
require(rjson)
target <- c('TMF', 'TMF', 'TMF','Evaporation','Mill Reclaim','Void Losses','Seepage')
source <- c('Precipitation & Run Off','Slurry','Other','TMF','TMF','TMF','TMF')
value <- c(638,1610,755,118,1430,466,2)
x <- data.frame(target,source,value)
sankeyPlot <- rCharts$new()
sankeyPlot$set(
data = x,
nodeWidth = 10,
nodePadding = 10,
layout = 32,
width = 1100,
height = 675,
units = "cubic metres",
title = "Sankey Diagram"
)
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot

在我的viewmodel中我已经

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

    }

我试过不放一个亲戚源,但它在我的有界物品(绑定到列表项目的物品)中寻找命令

我该如何解决? 感谢

1 个答案:

答案 0 :(得分:1)

Light Switch打败了我的原因,改为使用它。

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

Here is a related question