【问题标题】:WPF - Ticking checkbox when clicking anywhere on a datagrid rowWPF - 单击数据网格行上的任意位置时勾选复选框
【发布时间】:2017-03-27 15:17:53
【问题描述】:

图片显示了在我的数据网格中添加的一些项目的示例。现在我只能通过单击更改每行复选框,但我希望能够单击每行上的任意位置以更改该行复选框。

数据网格和行模板的 XAML:

  <DataGrid Grid.Row="1" SnapsToDevicePixels="True" BorderThickness="0" SelectedItem="{Binding Selected}" ItemsSource="{Binding Source={StaticResource ColViewSource},Mode=OneWay}"   AutoGenerateColumns="False" x:Name="dataGrid" HeadersVisibility="None" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">

        <DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <DataGridRowsPresenter></DataGridRowsPresenter>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>

            </GroupStyle>
        </DataGrid.GroupStyle>

    </DataGrid>

 <Style TargetType="DataGridRow">

     <Style.Triggers>
            <DataTrigger Binding="{Binding Type}" Value="TTC">
                <Setter Property="Template"  Value="{StaticResource ResourceKey=TTC}"/>
            </DataTrigger>

           </Style.Triggers>

    </Style>

<ControlTemplate x:Key="TTC">
        <Grid Style="{StaticResource rowGridStyle}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="30"/>
                <ColumnDefinition />
                <ColumnDefinition/>
                <ColumnDefinition/>


            </Grid.ColumnDefinitions>
            <TextBox Style="{StaticResource commentboxStyle}" Text="{Binding Comment,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.ColumnSpan="3" ></TextBox>
            <TextBlock Style="{StaticResource textblockStyle}" Text="{Binding Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.ColumnSpan="2"/>
            <Label Style="{StaticResource labelStyle}" Content="{Binding Text2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" HorizontalAlignment="Right" />
            <CheckBox Style="{StaticResource RectBox}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" HorizontalAlignment="Center" IsThreeState="True"/>
            <Path Style="{StaticResource dottedpathStyle}"/>

        </Grid>
    </ControlTemplate>

关于如何实现这一点的任何提示?

【问题讨论】:

  • 能否请您发布您的 DataGrid 的 XAML 标记?
  • @mm8 添加了 xaml
  • 你在 PreviewMouseUp 事件处理程序中做什么?
  • @mm8 实际上什么都没有,将其从代码中删除。 (更新^)

标签: c# wpf xaml checkbox datagrid


【解决方案1】:

您可以向您的DataGridRow 样式添加一个处理程序,将数据对象的IsChecked 属性设置为true

<Style TargetType="DataGridRow">
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnPreviewMouseLeftButtonDown" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Type}" Value="TTC">
            <Setter Property="Template"  Value="{StaticResource ResourceKey=TTC}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (!(e.Source is CheckBox))
    {
        DataGridRow row = sender as DataGridRow;
        YourDataClass dataObject = row.DataContext as YourDataClass;
        if (dataObject != null)
            dataObject.IsChecked = true;
    }
}

确保YourDataClass 实现INotifyPropertyChanged 接口并在IsChecked 属性的设置器中引发PropertyChanged 事件。

【讨论】:

    【解决方案2】:

    防止隐藏代码并使用System.Windows.Interactivity.Behaviour

    public class SelectedCheckBoxBehaviour : System.Windows.Interactivity.Behavior<FrameworkElement>
    {
        protected override void OnAttached()
        {
    
            AssociatedObject.MouseDown += AssociatedObject_MouseDown;
    
            base.OnAttached();
        }
    
        private void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement self = sender as FrameworkElement;
    
            if(self != null)
            {
                MyViewModel dataContext = self.DataContext as MyViewModel;
    
                dataContext.IsChecked = !dataContext.IsChecked;
            }
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
            base.OnDetaching();
        }
    }
    

    命名空间

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:behaviour="clr-namespace:WpfApplication.Behaviour" 
    
    
    <!-- ... --->
    
    
        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="30"/>
                <ColumnDefinition />
                <ColumnDefinition/>
                <ColumnDefinition/>
    
    
            </Grid.ColumnDefinitions>
            <TextBox Text="{Binding Comment,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Grid.ColumnSpan="3" ></TextBox>
            <TextBlock Text="{Binding Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.ColumnSpan="2"/>
            <Label  Content="{Binding Text2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" HorizontalAlignment="Right" />
            <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" HorizontalAlignment="Center" IsThreeState="True"/>
            <Path />
    
            <i:Interaction.Behaviors>
                <behaviour:SelectedCheckBoxBehaviour />
            </i:Interaction.Behaviors>
    
        </Grid>
    

    这应该可以 - Tut

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多