WPF在ViewModel中不执行按钮的绑定命令

时间:2019-10-24 05:11:09

标签: c# wpf xaml mvvm data-binding

我在wpf C#应用程序中具有以下xaml。我想将按钮绑定到视图模型中的ICommand。由于某些原因,绑定命令无法执行。

    <UserControl x:Class="UI.View.CustomizationView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:viewModel="clr-namespace:UI.ViewModel"
                 d:DataContext="{d:DesignInstance viewModel:CustomizationViewModel,
                                                  IsDesignTimeCreatable=True}"
                 d:DesignHeight="300"
                 d:DesignWidth="300"
                 mc:Ignorable="d">
        <Grid>
            <Button Content="Update Options" Background="LightBlue" Command="{Binding UpdateCommand}"/>
        </Grid>
    </UserControl>

这是我的ViewModel:

    public class CustomizationViewModel : UiViewModelBase
    {
        public ICommand UpdateCommand
        {
            get
            {
                if (updateCommand == null)
                {
                    updateCommand = new RelayCommand(() => this.HandleUpdateCommand());
                }
                return updateCommand;
            }
            set => this.Set(ref this.updateCommand, value);
        }

        private void HandleUpdateCommand()
        {
            // Do something but this function is never called when button is clicked
        }
    }

任何人都可以帮助解释为什么从未调用HandleUpdateCommand()吗?

0 个答案:

没有答案