设计代码
<DataGrid Width="860" Height="530" IsReadOnly="True" ColumnHeaderStyle="{StaticResource IDataGridColumnHeaderStyle}"
AutoGenerateColumns="False" ItemsSource="{Binding Materials}" >
<DataGrid.Resources>
<domain:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn IsReadOnly="False" Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" ElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnStyle}"
EditingElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnEditingStyle}">
<DataGridCheckBoxColumn.Header>
<Border Background="Transparent">
<CheckBox IsChecked="{Binding Data.IsAllSelected, Source={StaticResource DataContextProxy}}" />
</Border>
</DataGridCheckBoxColumn.Header>
<DataGridCheckBoxColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridCheckBoxColumn.HeaderStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="材料类型" Binding="{Binding type}" Width="*"/>
<DataGridTextColumn Header="材料名称" Binding="{Binding name}" Width="*"/>
<DataGridTextColumn Header="规格" Binding="{Binding guige}" Width="*"/>
<DataGridTextColumn Header="型号" Binding="{Binding xinghao}" Width="*"/>
<DataGridTextColumn Header="单位" Binding="{Binding danwei}" Width="*"/>
<DataGridTextColumn Header="换算系数" Binding="{Binding xishu}" Width="*"/>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource MaterialDesignDataGridCell}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
BindingProxy代码(在material light源码中拷贝过来的)
命名空间自行修改
using System.Windows;
namespace MaterialDesignDemo.Domain
{
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore() => new BindingProxy();
public object Data
{
get => GetValue(DataProperty);
set => SetValue(DataProperty, value);
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
}
ViewModel代码
namespace TruckScales.App.ViewModel
{
public class XuanzecailiaoViewModel : ViewModelBase
{
public XuanzecailiaoViewModel()
{
foreach (var model in Materials)
{
model.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(Material.IsSelected))
{
RaisePropertyChanged(nameof(IsAllSelected));
TruckScalesHelper.ViewModelLocator.Jinchangchengzhong.RefreshSelectMaterials();
}
};
}
}
private List<Material> _materials;
public List<Material> Materials
{
get
{
if (_materials == null)
{
_materials = new List<Material>();
_materials.Add(new Material() { type = "1", name = "1" });
}
return _materials;
}
}
private bool? isAllSelected;
public bool? IsAllSelected
{
get
{
var selected = Materials.Select(item => item.IsSelected).Distinct().ToList();
return selected.Count == 1 ? selected.Single() : (bool?)null;
}
set
{
if (value.HasValue)
{
SelectAll(value.Value, Materials);
Set(ref isAllSelected, value);
}
}
}
private static void SelectAll(bool select, IEnumerable<Material> models)
{
foreach (var model in models)
{
model.IsSelected = select;
}
}
}
public class Material : ViewModelBase
{
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { Set(ref isSelected, value); }
}
public string type { get; set; }
public string name { get; set; }
}
}