【问题标题】:How to use MVVM Icommand on button? [duplicate]如何在按钮上使用 MVVM Icommand? [复制]
【发布时间】:2016-06-20 14:42:35
【问题描述】:

我有一个从可观察集合 ComputerList 中加载项目的列表

  <ListView  x:Name="icTodoList" ItemsSource="{Binding ComputerList}" SelectedItem="{Binding SelectedComputer}"   Grid.Column="3" SelectionChanged="icTodoList_SelectionChanged_1">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="{Binding borderColor }" BorderThickness="2" Margin="0,0,0,1">
                                <Grid Margin="2" Height="auto" Width="auto">
 <Button Height="18" Command="{Binding RemoveComputer}"  HorizontalAlignment="Right" ToolTipService.ShowDuration="60000" Margin="0,1,38,0" x:Name="button1_Copy" VerticalAlignment="Top" Width="25" FontSize="11" Foreground="#FF6BADF6" Content="" BorderBrush="#FF6BADF6"  Grid.Column="8"/>
                                </Grid>                                
                            </Border>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

我的按钮单击应该从 observableCollection 中删除项目,所以我确实以这种方式构建了 Icommand 界面(简而言之)。

 class ComputerViewModel : ViewModelBase
    {
        CustomClass _customClass = new CustomClass();
        public readonly ObservableCollection<Model.Model.ControleData> _ComputerList = new ObservableCollection<Model.Model.ControleData>();
        public ObservableCollection<Model.Model.ControleData> ComputerList { get { return _ComputerList; } }
        public ComputerViewModel()
        {
            ComputerList.Add(new Model.Model.ControleData { ComputerName = "TESTMACHINE", titlePing = "online",borderColor = Genkai.BlueMain });
         //   TextBoxText = "init";
            _canExecute = true;



        }
  private ICommand _RemoveComputer;
        public ICommand RemoveComputer
        {
            get
            {
                return _RemoveComputer ?? (_RemoveComputer = new CommandHandler(() => RemoveComp(), _canExecute));
            }
        }
  private bool _canExecute;

  public void RemoveComp()
        {
            Debug.WriteLine("close Item");

        }
  public class CommandHandler : ICommand
        {
            private Action _action;
            private bool _canExecute;
            public CommandHandler(Action action, bool canExecute)
            {
                _action = action;
                _canExecute = canExecute;
            }
            public void Execute(object parameter)
            {
                _action();
            }

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

            public event EventHandler CanExecuteChanged;


        }
}

点击时不会触发 removecomp 操作。

但是在我的视图模型中它被解雇了

 var hwc = new CommandHandler(RemoveComp,true);

            if (hwc.CanExecute(this))

                hwc.Execute(this);

所以我想我在 WPF 视图中错过了一些东西。

【问题讨论】:

  • 是的,似乎同样的问题,但痛苦理解尝试 仍然没有触发
  • 不建议将CanExecute 作为布尔值传递,而是作为Func&lt;bool&gt; 传递,否则您以后无法轻松更改该值。您的命令类还需要提供一个可以调用的方法来引发CanExecuteChanged,当CanExecute 函数的返回值可能发生变化时需要触发该方法。

标签: c# wpf xaml mvvm icommand


【解决方案1】:

您正在尝试在数据模板中绑定 VM 命令。它找不到这个命令,因为它有不同的上下文。 尝试以这种方式绑定

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:TypeOfYourControlOrWindow}}, Path=DataContext.YourCommand}"

【讨论】:

  • 我在用户控件 ComputerView 中,如果尝试过的话:Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:ComputerView}} 不起作用
  • 获取信息列表视图之外的其他 cmd 工作 如果有帮助
  • 你最后忘记了命令 Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:ComputerView}}, Path=DataContext.RemoveComputer}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
相关资源
最近更新 更多