2009-04-17 34 views
13

Tôi có một DataGrid trong một hình thức WPF với một DataGridCheckBoxColumn, nhưng tôi đã không tìm thấy bất kỳ sự kiện nhấp chuột, Kiểm tra và đánh dấu cho nó ...sự kiện Click cho DataGridCheckBoxColumn

Là những sự kiện có sẵn cho DataGridCheckBoxColumn? Nếu không xin vui lòng đề nghị một số workaround tôi có thể sử dụng.

Trả lời

1

Sẽ không được truy cập thông qua DataGridCell riêng lẻ chứ không phải toàn bộ cột?

tôi biết những sự kiện này không phải là trực tiếp trên DataGridCell, nhưng có những CommandBindings phương pháp:

// Summary: 
    //  Gets a collection of System.Windows.Input.CommandBinding objects associated 
    //  with this element. A System.Windows.Input.CommandBinding enables command 
    //  handling for this element, and declares the linkage between a command, its 
    //  events, and the handlers attached by this element. 
    // 
    // Returns: 
    //  The collection of all System.Windows.Input.CommandBinding objects. 

Liệu sự giúp đỡ này?

0
<wpf:DataGridCheckBoxColumn Header="Cool?" Width="40" Binding="{Binding IsCool}"/> 
4

Mở rộng trên khái niệm DataGridCell được lưu ý ở trên, đây là cách chúng tôi sử dụng để làm việc.

... XAML ...

<DataGrid Grid.ColumnSpan="2" Name="dgMissingNames" ItemsSource="{Binding Path=TheMissingChildren}" Style="{StaticResource NameListGrid}" SelectionChanged="DataGrid_SelectionChanged"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn CellStyle="{StaticResource NameListCol}"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" Name="theCheckbox" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate>        
      </DataGridTemplateColumn> 
      <DataGridTextColumn Binding="{Binding Path=SKU}" Header="Album" /> 
      <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" "/> 
      <DataGridTextColumn Binding="{Binding Path=Pronunciation}" Header="Pronunciation" /> 
     </DataGrid.Columns> 
    </DataGrid> 

TheMissingChildren là một đối tượng ObservableCollection có chứa danh sách các yếu tố dữ liệu bao gồm một lĩnh vực boolean "Checked" mà chúng tôi sử dụng để cư DataGrid.

Mã SelectionChanged tại đây sẽ đặt boolean được chọn trong đối tượng TheMissingChildren bên dưới và kích hoạt lại danh sách các mục. Điều đó đảm bảo rằng hộp sẽ được đánh dấu chọn & hiển thị trạng thái mới cho dù bạn nhấp vào hàng nào. Nhấp vào hộp kiểm hoặc một nơi nào đó trong hàng sẽ bật/tắt kiểm tra.

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    DataGrid ThisGrid = (DataGrid)sender; 
    CheckedMusicFile ThisMusicfile = (CheckedMusicFile)ThisGrid.SelectedItem; 
    ThisMusicfile.Checked = !ThisMusicfile.Checked; 
    ThisGrid.Items.Refresh(); 
} 
1

Làm thế nào về điều này.

partial class SomeAwesomeCollectionItems : INotifyPropertyChanged 
{ 
    public event PropertyChanged; 
    protected void OnPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property); 
    } 

    private bool _IsSelected; 
    public bool IsSelected { get { return _IsSelected; } set { _IsSelected = Value; OnPropertyChanged("IsSelected"); } } 
} 

Sau đó, trong XAML

<DataGrid ItemsSource="{Binding Path=SomeAwesomeCollection"} SelectionMode="Single"> 
    <DataGrid.Resources> 
     <Style TargetType="{x:Type DataGridRow}" 
       BasedOn="{StaticResource {x:Type DataGridRow}}"> 
     <!--Note that you will probably need to base on other style if you have stylized your DataGridRow--> 
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" /> 
     </Style> 
    </DataGrid.Resources 
    <DataGrid.Columns> 
     <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" /> 
     <!--More Columns--> 
    </DataGrid.Columns> 
</DataGrid> 

Một lưu ý với cách tiếp cận này, tuy nhiên, là bạn có thể gặp vấn đề với ảo hóa và kiểm tra các mặt hàng không thanh toán bù trừ (không chắc chắn, chưa được thử nghiệm với SelectionMode =" Độc thân"). Nếu đúng như vậy, cách giải quyết đơn giản nhất mà tôi đã tìm được để làm việc là tắt ảo hóa, nhưng có lẽ có cách tốt hơn để giải quyết vấn đề cụ thể đó.

12

Trích dẫn từ William Han trả lời ở đây: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d

Nó chỉ đơn giản cho biết thêm một sự kiện vào cột. Đó là một giải pháp đơn giản.

Có lẽ bạn có thể sử dụng EventSetter như ví dụ dưới đây:

Markup:

<Window x:Class="DataGridCheckBoxColumnTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:DataGridCheckBoxColumnTest" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:People x:Key="People"/> 
    </Window.Resources> 
    <Grid> 
     <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/> 
       <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar"> 
        <DataGridCheckBoxColumn.CellStyle> 
         <Style> 
          <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/> 
         </Style> 
        </DataGridCheckBoxColumn.CellStyle> 
       </DataGridCheckBoxColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

Code:

using System; 
using System.Windows; 

namespace DataGridCheckBoxColumnTest 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     void OnChecked(object sender, RoutedEventArgs e) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 


namespace DataGridCheckBoxColumnTest 
{ 
    public class Person 
    { 
     public Person(string name, bool likeCar) 
     { 
      Name = name; 
      LikeCar = likeCar; 
     } 
     public string Name { set; get; } 
     public bool LikeCar { set; get; } 
    } 
} 

using System.Collections.Generic; 

namespace DataGridCheckBoxColumnTest 
{ 
    public class People : List<Person> 
    { 
     public People() 
     { 
      Add(new Person("Tom", false)); 
      Add(new Person("Jen", false)); 
     } 
    } 
} 
Các vấn đề liên quan