How to uncheck all CheckBox in side datatemplate o

2019-08-09 19:11发布

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!

1条回答
小情绪 Triste *
2楼-- · 2019-08-09 19:14

In my opinion, one of the solutions can pass by this:

  1. Have a property on the datacontext that is binded to the isselected property on the checkbox;
  2. On button click, pass the gridview itemsource in the CommandParameter, or if you bind the itemssource to a list in the datacontext use that list. Do a foreach and put the property IsSelected (that i said in 1) to false...The bind in the checkbox must be two way and implement the InotifyPropertyChanged.

If I was not clear in any point, please just tell me :)

Regards,

EDIT ----------------------------

This is my example working with the default controls (I don't have devexpress).

On the 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>

On the 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);

        }
    }
}

And I have this class with the implementation of 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;
        }


    }
}

Just analyse this and try to understand and adapt to your code.

Regards,

查看更多
登录 后发表回答