首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EventSetter在DataGridComboBoxColumn中抛出NullReferenceException

EventSetter在DataGridComboBoxColumn中抛出NullReferenceException
EN

Stack Overflow用户
提问于 2017-02-03 12:10:05
回答 1查看 686关注 0票数 1

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

代码语言:javascript
复制
<EventSetter Event="SelectionChanged" Handler="{Binding MyCommand}" />

但不幸的是我得到了NullReferenceException在InitializeComponent();

有什么问题吗?如果我删除上面的单行,程序就能工作。

代码语言:javascript
复制
<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));
      }
   }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-03 12:21:58

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

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42023994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档