首页 > 解决方案 > 如何正确使用 DelegateCommand?

问题描述

在调试模式下,从构造函数转到set方法,但在get方法中没有任何时候进入,我认为这是问题,但我不知道如何解决。

在 .xaml 文件中,我定义了按钮。

<dx:ThemedWindow
    x:Class="ProductsModule.View.ProductView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    Title="MenuView" Height="800" Width="1000">
    
    <Grid Margin="20" VerticalAlignment="Top" Background="White" Height="420" DataContext="{Binding Detail}">
        <Grid.Effect>
            <DropShadowEffect BlurRadius="20" ShadowDepth="1"/>
        </Grid.Effect>
        <StackPanel Margin="750 70 70 70" HorizontalAlignment="Left">
            <TextBlock Text="{Binding Name}" FontSize="18" Margin="0 5" Foreground="#FF6A6A6A"/>
            <Button Command="{Binding ShowCommand }">Add data</Button>
            
        </StackPanel>
        
    </Grid>
    
</dx:ThemedWindow>

在 .xaml.cs 文件中,我定义了数据上下文。

public SecondView()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }

查看模型

public ViewModel()
        {
            ShowCommand = new DelegateCommand(ShowMethod);
        }
        public ICommand ShowCommand
        {
            get;
            set;
        }
        private void ShowMethod()
        {
            OrderView orderView = new OrderView();
            orderView.Show();
        }

当我按下按钮时,不调用 ShowMethod() 并且不执行任何操作。

标签: c#wpf

解决方案


推荐阅读