在我看來,解決辦法之一可以通過此傳遞:
- 對被綁定到該複選框IsSelected屬性DataContext的一個屬性;
- 單擊按鈕時,在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;
}
}
}
就分析這一點,並嘗試理解並適應你的代碼。
問候,
複製 - http://stackoverflow.com/questions/16784265/how-to-uncheck-checkbox-on-button-click-in-wpf-using-c – DHN