2009-04-17 22 views
13

Mam DataGrid w formie WPF z DataGridCheckBoxColumn, ale nie znaleźliśmy żadnego zdarzenia click, sprawdzone i zaznaczone na to ...Kliknij wydarzenie dla DataGridCheckBoxColumn

Czy te dostępne dla DataGridCheckBoxColumn wydarzenia? Jeśli nie, zasugeruj jakieś obejście, które mógłbym zastosować.

Odpowiedz

1

Czy nie będą one dostępne za pośrednictwem indywidualnego DataGridCell zamiast całej kolumny?

znam te wydarzenia nie są bezpośrednio na DataGridCell, ale tam jest metoda CommandBindings:

// 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. 

Czy to pomoże?

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

Rozszerzając powyższą koncepcję DataGridCell, właśnie ją wykorzystaliśmy.

... 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 jest obiektem ObservableCollection, które zawiera listę elementów danych w tym polu wartości logicznej „zaznaczone”, którego używamy do wypełnienia DataGrid.

Kod SelectionChanged tutaj ustawi sprawdzoną boolean w podstawowym obiekcie TheMissingChildren i odpali odświeżenie listy elementów. To gwarantuje, że pole zostanie odznaczone & wyświetlać nowy stan bez względu na to, gdzie klikniesz wiersz. Kliknięcie pola wyboru lub w dowolnym miejscu w rzędzie spowoduje przełączenie czeku.

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

Co powiecie na coś takiego.

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"); } } 
} 

Następnie w 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> 

Jedna uwaga z tego podejścia jest jednak to, może napotkasz problemy z wirtualizacji i sprawdzone elementy nie wyczyszczenie (nie wiem, nie testowałem z SelectionMode =” Pojedynczy"). W takim przypadku najprostszym obejściem, które udało mi się znaleźć, jest wyłączenie wirtualizacji, ale być może jest lepszy sposób na obejście tego konkretnego problemu.

12

Cytat z odpowiedzi Williama Hana tutaj: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d

To po prostu dodaje zdarzenie do kolumny. To dobre proste rozwiązanie.

Być może można użyć EventSetter jak przykład poniżej:

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> 

Kod:

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)); 
     } 
    } 
} 
Powiązane problemy