Reputation: 3907
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
Upvotes: 1
Views: 705
Reputation: 9723
Light Switch beat me to the reason, use this instead.
Command="{Binding DataContext.Reconnect, ElementName=listBox}"
Upvotes: 1