WPF. Команда контекстного меню
#39841570
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Russia, Moscow
Сообщения: 100
|
|
Как в нижеприведенном случае правильно прописать вызов команды из контекстного меню в XAML? (Вызов из обычного меню работает.)
фрагмент Style.xaml: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:MainFrame.ViewModels">
<Style TargetType="{x:Type TabItem}">
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
<Setter Property="Uid" Value="{Binding Uid}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Закрыть вкладку"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}},
/*Здесь не вызывается*/ Path=PlacementTarget.Tag[{viewModels:ViewModelComands.CloseTabCommand}]}"
/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<!-- this is the header template-->
<Grid x:Name="TabItemGrid" HorizontalAlignment="Stretch" Background="#FFE5E5E5" Margin="2,0,2,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="53*"/>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<ContentPresenter Content="{Binding Header}" />
<Button Name="ButtonCloseTabItem"
/*Здесь вызывается*/ Command="viewModels:ViewModelComands.CloseTabCommand"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
фрагмент MainViewModel.cs: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.
namespace MainFrame.ViewModels
{
internal class MainViewModel : ViewModel
{
public ObservableCollection<TabItemViewModel> Tabs
{
get { return Get(() => Tabs); }
set { Set(() => Tabs, value); }
}
private void Initialize(StreamingContext streamingContext = default(StreamingContext))
{
this[ApplicationCommands.Close].Executed += (sender, args) => Application.Current.Shutdown();
this[ViewModelComands.CloseTabCommand].Executed += (sender, args) => CloseTab(sender, args);
}
private void CloseTab(object sender, RoutedEventArgs e)
{
// ищем родительскую вкладку
TabItem tabItem = ParentControl.FindParent<TabItem>((Control)e.OriginalSource);
if (tabItem != null)
{
var select = Tabs.Where(i => i.Uid.ToString() == tabItem.Uid.ToString()).ToList();
foreach (var item in select)
{
Tabs.Remove(item);
}
}
}
}
}
фрагмент: ViewModelComands.cs 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
namespace MainFrame.ViewModels
{
class ViewModelComands
{
static ViewModelComands()
{
CloseTabCommand = new RoutedUICommand("Закрыть вкладку", "CloseTab", typeof(ViewModelComands));
}
public static RoutedCommand CloseTabCommand { get; }
}
}
|
|