Wpf Datagrid 设置了模板 checkbox 如何一次选中?
2个回答
展开全部
我写了个demo,希望对你有用。
前台:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="dg" Grid.Row="0" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Checked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Id,Mode=TwoWay}"/>
<DataGridTextColumn Binding="{Binding Name,Mode=TwoWay}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="全选" Width="50" Height="25" Grid.Row="1" Click="Button_Click"/>
</Grid>
后台:
public partial class MainWindow : Window
{
ObservableCollection<Exp> explist = new ObservableCollection<Exp>();
public MainWindow()
{
InitializeComponent();
explist.Add(new Exp() { Id = 1, Name = "a", Checked = false });
explist.Add(new Exp() { Id = 2, Name = "b", Checked = false });
explist.Add(new Exp() { Id = 3, Name = "c", Checked = false });
dg.ItemsSource = explist;
}
public class Exp : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
private bool Checked1;
public bool Checked
{
set
{
Checked1 = value;
OnPropertyChanged("Checked");
}
get
{
return Checked1;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (Exp item in explist)
{
item.Checked = true;
}
}
}
前台:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="dg" Grid.Row="0" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Checked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Id,Mode=TwoWay}"/>
<DataGridTextColumn Binding="{Binding Name,Mode=TwoWay}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="全选" Width="50" Height="25" Grid.Row="1" Click="Button_Click"/>
</Grid>
后台:
public partial class MainWindow : Window
{
ObservableCollection<Exp> explist = new ObservableCollection<Exp>();
public MainWindow()
{
InitializeComponent();
explist.Add(new Exp() { Id = 1, Name = "a", Checked = false });
explist.Add(new Exp() { Id = 2, Name = "b", Checked = false });
explist.Add(new Exp() { Id = 3, Name = "c", Checked = false });
dg.ItemsSource = explist;
}
public class Exp : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
private bool Checked1;
public bool Checked
{
set
{
Checked1 = value;
OnPropertyChanged("Checked");
}
get
{
return Checked1;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (Exp item in explist)
{
item.Checked = true;
}
}
}
追问
感谢答复,但我需要的效果是单个checkbox,选中一行,checkbox就选中,再单击就不选中。默认情况下,要选中单个的话,是要点2次
追答
那重写checkbox的click事件也许行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询