1

I have a CheckBox in GridControl Column. After performing some operation the selected checkboxes inside GridControl must be UNCHECKED on button click in WPF. Any idea?

<dxg:GridControl Name="grdInfill"  Height="700" VerticalAlignment="Center">
    <dxg:GridControl.Columns>
        <dxg:GridColumn  AllowEditing="True">
            <dxg:GridColumn.CellTemplate>
                <DataTemplate>
                    CheckBox Name="chkSelect"   HorizontalAlignment="Center" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected,Mode=TwoWay}"  Checked="CheckEdit_Checked" Unchecked="CheckEdit_Unchecked"/>
                 </DataTemplate>
             </dxg:GridColumn.CellTemplate>
         </dxg:GridColumn>
     </dxg:GridControl.Columns>
     <dxg:GridControl.View>
         <dxg:TableView Name="grdInfillInner"  ShowTotalSummary="True" AutoWidth="True" 
             DetailHeaderContent="True"  ShowIndicator="False" ShowGroupPanel="False" 
             CellValueChanging="grdInfillInner_CellValueChanging">
             <!--GroupRowTemplate="{StaticResource descriptionHeader}"-->
         </dxg:TableView>
     </dxg:GridControl.View>
</dxg:GridControl>
<Button Name="BtnClearAllCheckbox" Content="Clear All Checkbox" Height="20" Width="80" />

Help Appreciated!

4

1 回答 1

3

在我看来,其中一种解决方案可以通过:

  1. 在 datacontext 上有一个属性,该属性绑定到复选框上的 isselected 属性;
  2. 单击按钮时,在 CommandParameter 中传递 gridview itemsource,或者如果您将 itemssource 绑定到 datacontext 中的列表,请使用该列表。做一个foreach并将属性IsSelected(我在1中说的)设置为false ...复选框中的绑定必须是两种方式并实现InotifyPropertyChanged。

如果我有任何不清楚的地方,请告诉我:)

问候,

编辑 - - - - - - - - - - - - - -

这是我使用默认控件的示例(我没有 devexpress)。

在 XAML 上:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="checkBoxTemplate">
            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <ListView ItemsSource="{Binding listExample}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn CellTemplate="{StaticResource checkBoxTemplate}"></GridViewColumn>
                        <GridViewColumn  DisplayMemberBinding="{Binding Test1}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Content="Uncheck all" Click="Button_Click"></Button>
        </StackPanel>
    </Grid>
</Window>

在 CodeBehind 上:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Example> listExample { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.listExample = new List<Example>();
            listExample.Add(new Example { IsChecked = false, Test1 = "teste" });
            listExample.Add(new Example {IsChecked = false, Test1 = "TTTTT!" });
            DataContext = this;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.listExample.ForEach(x => x.IsChecked = false);

        }
    }
}

我有这个类与 INotifyPropertyChanged 的​​实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplication1
{
    public class Example : INotifyPropertyChanged
    {
        private bool isChecked;
        public bool IsChecked { get { return isChecked; } set { SetField(ref isChecked, value, "IsChecked"); } }

        public string Test1 { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }


    }
}

只需对此进行分析并尝试理解并适应您的代码。

问候,

于 2013-05-28T09:04:58.893 回答