2013-05-28 43 views
1

我在GridControl列中有一個CheckBox。 執行一些操作後,GridControl內的選中複選框必須在WPF的按鈕單擊時未選中。任何想法?如何取消選中gridcontrol的side datatemplate中的所有CheckBox按鈕在使用C#的WPF中單擊?

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

幫助讚賞!

+0

複製 - http://stackoverflow.com/questions/16784265/how-to-uncheck-checkbox-on-button-click-in-wpf-using-c – DHN

回答

3

在我看來,解決辦法之一可以通過此傳遞:

  1. 對被綁定到該複選框IsSelected屬性DataContext的一個屬性;
  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> 

在代碼隱藏:

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


    } 
} 

就分析這一點,並嘗試理解並適應你的代碼。

問候,

+0

的IsSelected是布爾屬性我已綁定的字段。我是新來的wpf,那麼請你提供上面的樣本嗎? –

+0

我會盡量做出一個快速的例子。 :)給我幾分鐘;) – sexta13

+0

感謝您給予您的時間!我非常感謝你的工作!我通過分析您的代碼段在我的代碼中進行了某種程度的修改!並非常努力...!非常感謝! :) –

相關問題