2009-07-16 2 views
1

WPF Windows 응용 프로그램에서 WPF 툴킷의 DataGrid를 사용하고 있습니다. 행 헤더에 행을 선택하는 데 사용되는 확인란 열을 만드는 데이터 형식이 있습니다.DataGrid의 체크 박스 열 머리글 클릭시 팝업 메뉴를 표시하는 방법은 무엇입니까?

체크 박스 열의 머리글 행 (그리드에서 가장 왼쪽 맨 위의 셀)을 클릭하면 그리드의 모든 확인란을 선택하여 모든 행을 선택합니다. XAML

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" > 

<toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding}" 
AutoGenerateColumns="True" SelectionMode="Extended" CanResizeRows="False"> 
    <toolkit:DataGrid.RowHeaderTemplate> 
     <DataTemplate> 
      <Grid> 
       <CheckBox IsChecked="{ 
       Binding Path=IsSelected, 
       Mode=TwoWay, 
       RelativeSource={RelativeSource FindAncestor, 
       AncestorType={x:Type toolkit:DataGridRow}}}" 
       /> 
      </Grid> 
     </DataTemplate> 
    </toolkit:DataGrid.RowHeaderTemplate> 
</toolkit:DataGrid> 
</Window> 

에서

관련 부분은 지금이 클릭을 처리하는 방법을 알고 싶어요. 나는 이것을 처리하고 팝업 메뉴을 대신 보여줄 계획입니다.

어떤 컨트롤의 클릭 이벤트로 연결해야합니까?

답 : 그래서 (예) 같은 이벤트를 처리 할 SelectAllCommand를 사용하여

datagrid.CommandBindings.Add(new CommandBinding(
Microsoft.Windows.Controls.DataGrid.SelectAllCommand, 
OnSelectAll, CanExecuteSelectAll)); 
public void OnSelectAll(object sender, ExecutedRoutedEventArgs e) 
    { 
     MessageBox.Show("All Selected"); 
    } 
public void CanExecuteSelectAll(object sender, CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = true; 
     Console.WriteLine("Can I execute?"); 
    } 

답변

1

당신은 데이터 그리드의 ControlTemplate을 확장하면 당신이 SelectAllCommand에 바인딩 된 버튼이 있음을 알 수있다.

<Window x:Class="WpfApplication1.Window3" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
Title="Window3" Height="500" Width="500" 
Loaded="Window_Loaded"> 
<Window.Resources> 
    <Style x:Key="DataGridStyle1" TargetType="{x:Type toolkit:DataGrid}"> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="#FF688CAF"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type toolkit:DataGrid}"> 
        <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
         <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False"> 
          <ScrollViewer.Template> 
           <ControlTemplate TargetType="{x:Type ScrollViewer}"> 
            <Grid> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition Width="Auto"/> 
              <ColumnDefinition Width="*"/> 
              <ColumnDefinition Width="Auto"/> 
             </Grid.ColumnDefinitions> 
             <Grid.RowDefinitions> 
              <RowDefinition Height="Auto"/> 
              <RowDefinition Height="*"/> 
              <RowDefinition Height="Auto"/> 
             </Grid.RowDefinitions> 
             <Button Command="{x:Static toolkit:DataGrid.SelectAllCommand}" 
             Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}" Focusable="False"> 
              <Button.Template> 
               <ControlTemplate TargetType="{x:Type Button}"> 
                <Grid> 
                 <Rectangle x:Name="Border" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SnapsToDevicePixels="True"/> 
                 <Polygon x:Name="Arrow" Fill="Black" Stretch="Uniform" HorizontalAlignment="Right" Margin="8,8,3,3" VerticalAlignment="Bottom" Opacity="0.15" Points="0,10 10,10 10,0"/> 
                </Grid> 
                <ControlTemplate.Triggers> 
                 <Trigger Property="IsMouseOver" Value="True"> 
                  <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/> 
                 </Trigger> 
                 <Trigger Property="IsPressed" Value="True"> 
                  <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/> 
                 </Trigger> 
                 <Trigger Property="IsEnabled" Value="False"> 
                  <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/> 
                 </Trigger> 
                </ControlTemplate.Triggers> 
               </ControlTemplate> 
              </Button.Template> 
              <Button.Visibility> 
               <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}"> 
                <Binding.ConverterParameter> 
                 <toolkit:DataGridHeadersVisibility>All</toolkit:DataGridHeadersVisibility> 
                </Binding.ConverterParameter> 
               </Binding> 
              </Button.Visibility> 
             </Button> 
             <toolkit:DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1"> 
              <toolkit:DataGridColumnHeadersPresenter.Visibility> 
               <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}"> 
                <Binding.ConverterParameter> 
                 <toolkit:DataGridHeadersVisibility>Column</toolkit:DataGridHeadersVisibility> 
                </Binding.ConverterParameter> 
               </Binding> 
              </toolkit:DataGridColumnHeadersPresenter.Visibility> 
             </toolkit:DataGridColumnHeadersPresenter> 
             <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.ColumnSpan="2" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/> 
             <ScrollBar x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Grid.Column="2" Grid.Row="1" Maximum="{TemplateBinding ScrollableHeight}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"/> 
             <Grid Grid.Column="1" Grid.Row="2"> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}"/> 
               <ColumnDefinition Width="*"/> 
              </Grid.ColumnDefinitions> 
              <ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}"/> 
             </Grid> 
            </Grid> 
           </ControlTemplate> 
          </ScrollViewer.Template> 
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </ScrollViewer> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsGrouping" Value="True"> 
       <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <toolkit:DataGrid Name="dataGrid" 
         ItemsSource="{Binding}" 
         CanUserResizeRows="False" 
         SelectionMode="Extended" 
         AutoGenerateColumns="True" Style="{DynamicResource DataGridStyle1}"> 
     <toolkit:DataGrid.RowHeaderTemplate> 
      <DataTemplate> 
       <Grid> 
        <CheckBox IsChecked="{ 
         Binding Path=IsSelected, 
         Mode=TwoWay, 
         RelativeSource={RelativeSource FindAncestor, 
         AncestorType={x:Type toolkit:DataGridRow}}}" 
         /> 
       </Grid> 
      </DataTemplate> 
     </toolkit:DataGrid.RowHeaderTemplate> 
    </toolkit:DataGrid> 
</Grid> 

당신은 당신의 자신의 논리를 수행하거나 다른 컨트롤 버튼을 대체하는 버튼의 명령을 무시할 수

.