Команда связывания внутри DataTemplate в WPF C # - PullRequest
0 голосов
/ 18 сентября 2018

У меня есть тег кнопки внутри моего шаблона данных.Я хочу вызвать команду, как только я нажму на кнопку.Если кнопка находится за пределами шаблона данных, моя кнопка может работать.Я пробовал несколько решений, таких как:

<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btnUpdate_Click" 
                                    Command="{Binding Path=DataContext.updateProductionLineConfigCommand, 
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>

Это моя ViewModel:

public UpdateProductionLineConfigCommand updateProductionLineConfigCommand { get; set; }

    public ProductionLineConfigViewModel()
    {
        ProductionLineConfig = new ProductionLineConfig();
        newProductionLineConfigCommand = new NewProductionLineConfigCommand(this);
        updateProductionLineConfigCommand = new UpdateProductionLineConfigCommand(this);
    }

Это мой командный класс:

 public ProductionLineConfigViewModel VM { get; set; }

    public event EventHandler CanExecuteChanged;

    public UpdateProductionLineConfigCommand(ProductionLineConfigViewModel vm)
    {
        VM = vm;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        VM.updateProductionLineConfig();

    }

Может кто-нибудь, пожалуйста, помогите мнена этом.Почему я не могу связать свои команды с моей кнопкой?

1 Ответ

0 голосов
/ 19 сентября 2018

Это решение, которое решило мою проблему.

<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" 
Click="btnUpdate_Click" Command="{Binding DataContext.updateProductionLineConfigCommand, 
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:production_line_config_home}}}"/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...