2015-05-22 111 views
1

我有,有一個Telerik的WPF的觀點:定義爲不能綁定正確的命令視圖模型

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

    } 
RadListBox在每個項目相關聯的文本菜單

我得到的錯誤是

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

我試着不要把一個的RelativeSource,但它看起來在我的綁定命令ed項目(綁定到listitem的項目)

我該如何解決它? 謝謝

+1

的ContextMenu採取的ItemsControl的DataContext的,所以它不能直接訪問視圖模型。它也不是VisualTree的一部分,所以你不能執行RelativeSource綁定。看我的帖子http://stackoverflow.com/questions/30104193/how-to-bind-command-into-contextmenu-in-datatemplate/30105635#30105635 –

回答

1

燈開關擊敗了我的原因,使用此代替。

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

Here is a related question

+0

我總是得到System.Windows.Data錯誤:4:找不到源代碼用於綁定引用'ElementName = listBox'。 BindingExpression:路徑= DataContext.Reconnect;的DataItem = NULL;目標元素是'EventToCommand'(HashCode = 19341321);目標屬性是'Command'(類型'ICommand') System.Windows.Data錯誤:4:找不到與參考'ElementName = listBox'綁定的源。 BindingExpression:路徑= DataContext.Reconnect;的DataItem = NULL;目標元素是'EventToCommand'(HashCode = 8260362);目標屬性是'命令'(鍵入'ICommand') – advapi

相關問題