【问题标题】:Add attached event to DataGridColumn将附加事件添加到 DataGridColumn
【发布时间】:2015-03-12 19:56:26
【问题描述】:

我正在尝试在网格中使用 DataGridCheckBoxColumn,但由于某种原因,我注意到它没有选中或未选中的事件。

我试图通过创建一个继承 DataGridCheckBoxColumn 的自定义 CBColumn 类来添加附加事件。

我遇到的问题是我不确定如何将处理程序添加到公开的属性,因为 DataGridCheckBoxColumn 不是从 UIElement 派生的。

因此 AddHandler 和 RemoveHandler 在此代码块中不可用:

 public event RoutedEventHandler Checked
    {
        add { AddHandler(CheckedEvent, value); }
        remove { RemoveHandler(CheckedEvent, value); }
    }

关于如何做到这一点的任何想法?我看了一遍都没有运气。

编辑:我正在使用 MVVM,因此我需要尽可能避免使用 Code Behind。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    Click event for DataGridCheckBoxColumn

    <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar">
                        <DataGridCheckBoxColumn.CellStyle>
                            <Style>
                                <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
                            </Style>
                        </DataGridCheckBoxColumn.CellStyle>
                    </DataGridCheckBoxColumn>
    

    【讨论】:

    • 抱歉,我应该指定我在 MVVM 中执行此操作。我尝试使用 Handler 属性上的 Binding 这样做,但它不起作用。这就是我想覆盖 DataGridCheckBoxColumn 并使用附加事件的原因。
    • 我还应该补充一点,在 Handler 属性上使用绑定会引发空引用异常。
    • 所以你的绑定不会触发。选择项目时是否可以打断点?我将尝试让它在 MVVM 中工作并发布我的发现。
    【解决方案2】:

    这是另一种代码解决方案。这确实很粗略,但它演示了选中的框,并在文本框中显示了选中内容的数值。

    <Window x:Class="DataGridCheckBoxItemTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:DataGridCheckBoxItemTest"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.DataContext>
        <vm:DataGridTestVM />
    </Window.DataContext>
    
    <Grid>
        <DataGrid ItemsSource="{Binding Source}" 
                  SelectedValue="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  Margin="10">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Test Checked" Binding="{Binding S}"/>
            </DataGrid.Columns>
        </DataGrid>
    
        <TextBox HorizontalAlignment="Left" 
                 Text="{Binding Test, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                 Height="39" 
                 Margin="20,244,0,0" 
                 TextWrapping="Wrap" 
                 VerticalAlignment="Top" 
                 Width="237"/>
    </Grid>
    

    namespace DataGridCheckBoxItemTest
    {
     public class DataGridTestVM : INotifyPropertyChanged
     {
       ObservableCollection<Source> source;
       Source s;
       int test;
    
    public DataGridTestVM()
    {
      source = new ObservableCollection<Source>();
      for (int i = 0; i < 10; i++)
      {
        s = new Source();
        s.test = i;
        source.Add(s);
      }
    }
    
    public ObservableCollection<Source> Source
    {
      get
      {
        return source;
      }
    
      set
      {
        source = value;
        OnPropertyChanged("Source");
      }
    }
    
    public int Test
    {
      get
      {
        return test;
      }
    
      set
      {
        test = value;
        OnPropertyChanged("Test");
      }
    }
    
    public Source Selected
    {
      get
      {
        return s;
      }
    
      set
      {
        s = value;
        Test = s.test;
        OnPropertyChanged("Selected");
      }
    }
    
       public event PropertyChangedEventHandler PropertyChanged;
       public void OnPropertyChanged(string name)
       {
         if (this.PropertyChanged != null)
         {
           this.PropertyChanged(this, new PropertyChangedEventArgs(name));
         }
       }
     }
    
     public class Source
     {
       public int test;
     }
    }
    

    【讨论】:

    • 这有帮助还是您自己解决了这个问题。如果是这样,请将其标记为已解决。谢谢
    • 我真的很想使用选中/未选中的命令。但我决定走另一条路。
    【解决方案3】:

    我最终只是恢复到 DataGridTemplateColumn 并使用其中的复选框控件。似乎没有办法做我想做的事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多