DataGridComboBoxColumn中的EventSetter引发NullReferenceException [英] EventSetter in DataGridComboBoxColumn throws NullReferenceException

查看:107
本文介绍了DataGridComboBoxColumn中的EventSetter引发NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SelectionChanged绑定到以下代码中的命令.

I'm trying to bind the SelectionChanged to a command in the code below.

<EventSetter Event="SelectionChanged" Handler="{Binding MyCommand}" />

但是不幸的是,我在 InitializeComponent();

可能是什么问题?如果删除上面的单行,该程序将起作用.

What can be the problem? The program works if I remove the above single line.

<StackPanel>

    <DataGrid ItemsSource="{Binding Items}" 
              AutoGenerateColumns="False">
        <DataGrid.Columns>

            <DataGridTextColumn Header="Name" 
                                Binding="{Binding Name}"/>

            <DataGridComboBoxColumn Header="Color" 
                                    SelectedItemBinding="{Binding Color}">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Colors}"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Colors}"/>

                        <EventSetter Event="SelectionChanged" Handler="{Binding MyCommand}" />


                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

    <Button Command="{Binding MyCommand}" Width="100" Height="100" Content="Change"/>
</StackPanel>


   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();

         this.DataContext = new Data();
      }
   }


   public class Data
   {
      /// <summary>
      /// A collection that stores the data required 
      /// to populate the <seealso cref="DataGrid"/> for sheets in paste window.
      /// </summary>
      private ObservableCollection<Item> _items;
      public ObservableCollection<Item> Items
      {
         get { return _items; }

      }

      private ICommand _myCommand;
      public ICommand MyCommand
      {
         get
         {
            return _myCommand ?? (_myCommand = new CommandHandler(() => Change(), _canExecute));
         }
      }
      private bool _canExecute;

      public Data()
      {
         _canExecute = true;
         _items = new ObservableCollection<Item>();
         _items.Add(new Item("A"));
         _items.Add(new Item("B"));
      }

      public void Change()
      {
         _items[0].Name = "D";
      }
   }

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

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

      public event EventHandler CanExecuteChanged;

      public void Execute(object parameter)
      {
         _action();
      }
   }

   public class Item : INotifyPropertyChanged
   {
      private string _name;
      public string Name
      {
         get { return _name; }
         set
         {
            _name = value;
            RaisePropertyChanged("Name");
         }
      }
      public string Color { get; set; }


      private IList<string> _colors;

      public event PropertyChangedEventHandler PropertyChanged;

      public IList<string> Colors
      {
         get { return _colors; }
      }


      public Item(string name)
      {
         _name = name;
         _colors = new List<string> { "Green", "Blue" };
         Color = _colors[0];
      }

      private void RaisePropertyChanged(string propertyName)
      {
         var handler = PropertyChanged;
         if (handler == null) return;

         handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

推荐答案

EventSetter希望您指出隐藏代码文件(xaml.cs)中的现有事件.它不适用于绑定.因此,请在MainWindow.xaml.cs中创建相应的事件处理程序,并在EventSetter中进行指示,或使用Blend,MvvmLight

EventSetter expects from you to indicate existing event from codebehind file (xaml.cs). It doesn't work with bindings. So create corresponding event handler inside MainWindow.xaml.cs and indicate it in EventSetter or use Interaction.Triggers from Blend, MvvmLight

<Window
 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
 xmlns:command="http://www.galasoft.ch/mvvmlight" >
 <i:Interaction.Triggers>
          <i:EventTrigger EventName="Loaded">
            <command:EventToCommand
              Command="{Binding MyCommand}"
              PassEventArgsToCommand="False" />
          </i:EventTrigger>
 </i:Interaction.Triggers>
</Window>

这篇关于DataGridComboBoxColumn中的EventSetter引发NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆