一个图形用户界面元素,用于显示数据的表格视图。
MUI Data-grid-pro React 组件中点击时如何在行下显示自定义组件?
我正在尝试在 MUI DataGrid 中添加一个功能,如果您单击一行,它会展开更多信息, 这是我尝试过的演示 但我无法将其自定义为具有行的完整宽度并具有...
我有一个数据网格绑定到数据集,我想在页脚中显示填充整数的列的平均结果。 我认为,我可以想到两种方法: 1.“使用...
@mui/x-data-grid 中的 ValueGetter 函数接收未定义的参数
我正在使用 @mui/x-data-grid 版本 7.3.0,我的列定义中的 valueGetter 函数存在问题。 valueGetter 函数应该接收的 params 对象是
尽管 Filterable 设置为 true,MudDataGrid 未在 Blazor 应用程序中显示筛选选项
我正在使用 Blazor Web App .NET 8 和最新的 MudBlazor 服务。设置 Filterable = true 后,过滤器图标出现在我的表格上,但是单击时,没有显示任何内容。我正在关注...
DataGrid Mudblazor 中的实际值和显示值不同
我正在尝试实现一个通用数据网格,它将与我将发送到组件的任何数据一起使用。我正在 blazor 中编写代码,特别是使用 MudBlazor 框架。 所以我有一个问题
我有一个 DataGrid(称为 TheGrid),我想在其上实现复制和粘贴功能。复制功能很好用,但我不知道如何实现粘贴。我只需要得到...
MudBlazor DataGrid 问题 - 如何控制自动生成的保存按钮的操作
我正在尝试自学 MudBlazor,因为我厌倦了自己处理所有 CSS。它是如此美好和强大,我很喜欢它! 不过我确实遇到了一个小问题。假设您想使用
尽管 IsEnabled 设置为 true,DataGrid 中的按钮仍被禁用
目标是将启用的按钮插入到 DataGrid 的列中。 在以下 XAML 代码片段中,将 Button 插入到 DataGrid 中,并将 IsEnabled 属性设置为 true。 目标是将启用的 Button 插入到 DataGrid 的列中。 在以下 XAML 代码片段中,将 Button 插入到 DataGrid 中,并将 IsEnabled 属性设置为 true。 <DataGrid Name="DgData" HorizontalAlignment="Left" Height="306" Margin="10,119,0,0" VerticalAlignment="Top" Width="637" Background="White"> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Self},Path=IsNewItem,Mode=OneWay}" /> </Style> </DataGrid.RowStyle> <DataGrid.Columns> <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Id" IsReadOnly="True" Visibility="Hidden"/> <DataGridTextColumn Header="Date" Binding="{Binding Date}" IsReadOnly="True" Width="100" CanUserResize="False"/> <DataGridTextColumn Header="Domain name" Binding="{Binding DomainName}" IsReadOnly="True" Width="105" CanUserResize="False"/> <DataGridTextColumn Header="Type" Binding="{Binding Relevance}" IsReadOnly="True" Width="65" CanUserResize="False"/> <DataGridTextColumn Header="Category" Binding="{Binding DomainName}" IsReadOnly="True" Width="65" CanUserResize="False"/> <DataGridTextColumn Header="Description" Binding="{Binding Content}" IsReadOnly="True" Width="200" CanUserResize="False"/> <DataGridTemplateColumn MinWidth="100" Header="Note" CanUserSort="True" CanUserResize="False"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Name="BtnShowNotes" IsEnabled="True" Click="BtnShowNotes_OnClick">Show</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 但是,执行应用程序时,该按钮被禁用。 是什么导致了这个问题? 您的 RowStyle 将整行的 IsEnabled 属性设置为 false。删除此一项,Button 应启用: <DataGrid Name="DgData" HorizontalAlignment="Left" Height="306" Margin="10,119,0,0" VerticalAlignment="Top" Width="637" Background="White"> <DataGrid.Columns> <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Id" IsReadOnly="True" Visibility="Hidden"/> <DataGridTextColumn Header="Datum" Binding="{Binding Date}" IsReadOnly="True" Width="100" CanUserResize="False"/> <DataGridTextColumn Header="Doménové jméno" Binding="{Binding DomainName}" IsReadOnly="True" Width="105" CanUserResize="False"/> <DataGridTextColumn Header="Druh" Binding="{Binding Relevance}" IsReadOnly="True" Width="65" CanUserResize="False"/> <DataGridTextColumn Header="Kategorie" Binding="{Binding DomainName}" IsReadOnly="True" Width="65" CanUserResize="False"/> <DataGridTextColumn Header="Popis" Binding="{Binding Content}" IsReadOnly="True" Width="200" CanUserResize="False"/> <DataGridTemplateColumn MinWidth="100" Header="Poznámky" CanUserSort="True" CanUserResize="False"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Name="BtnShowNotes" IsEnabled="True" Click="BtnShowNotes_OnClick">Zobrazit</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
MudSwitch 的 MudBlazor DataGrid 分组
我正在尝试通过切换 MudSwitch 来切换 MudDataGrid 中分组的列。默认情况下,它按实体分组并显示项目计数。我希望能够切换它以显示...
使用MUI DataGrid,如何在Change时更新单元格状态?
我需要什么:我正在使用 MUI DataGrid > 5。我需要单元格中填充的数据自动更改状态(onChange)。此时此刻,我有一个 useEffect 正在监听这些 c...
在 table.tsx 的这段代码上我收到以下渲染 我的所有数据都在 logbookData 中,数组中有数百个数据条目,但 DataGrid 始终只呈现相同的数据条目。 日志数据...
有什么方法可以保存Mui DataGrid Toolbar的状态吗?
我正在使用带有默认工具栏的 React Mui DataGrid 组件。用户可以使用工具栏以多种方式配置网格,例如排序顺序/隐藏列/过滤器/更改密度等。是...
我希望在 WPF 应用程序中的 DataGrid 中有一个具有透明背景的列。标题和单元格本身需要透明,但其中的内容不透明,这将是......
在 Github Codespace 中安装 scikit-fda 时出错(fdasrsf 构建问题)
我正在尝试在 github 代码空间上的脚本中导入一个库,即 skfda。 该包不存在,所以我尝试 pip install,我尝试 conda,我尝试从 github 克隆 repo ...
我需要将尺寸为(12,1800,3600)(时间经纬度)的精细矩阵变量(称为“fine_var”)重新网格化为粗矩阵变量(称为“coarse_var”),尺寸为(12,180,...
MUI DataGridPro useResizeContainer - 网格的父级具有空宽度
我恳求你帮助我,因为我已经寻找答案3天了,但我无法解决这个问题。 问题: MUI:useResizeContainer - 网格的父级具有空宽度。 你需要做...
使用 TemplateColumns 将 WPF DataGrid 绑定到 DataTable
我已经尝试了一切但一无所获,所以我希望有人能给我一个顿悟的时刻。 我根本无法获得成功提取数据网格中数据的绑定。 我有一个数据表,可以...
MUI Datagrid:行分组,详细信息显示在同一单元格中
我正在使用 datagrid mui 行分组。这是代码。 常量报告 = () => { const apiRef = useGridApiRef(); const initialState = useKeepGroupedColumnsHidden({ api参考, 初始状态...
将Cell中具有CustomClass的DataTable绑定到DataGrid WPF
我有数据网格。 我有数据网格。 <DataGrid ItemsSource="{Binding DataView}" x:Name="AttributeGrid" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" EnableColumnVirtualization="False" EnableRowVirtualization="False" DataContextChanged="AttributeGrid_DataContextChanged"> </DataGrid> 对于该 DataGrid 的单元格,我有自定义样式 <Style x:Key="DataGridDataViewCustomCellStyle" TargetType="{x:Type DataGridCell}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="DataGridCell"> <Grid> <ComboBox x:Name="ComboBoxCondition" IsEditable="True" ItemsSource="{Binding Values}" Visibility="Collapsed" SelectedValue="{Binding SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ValueID" DisplayMemberPath="ValueStr"/> <TextBox x:Name="TextBoxCondition" Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="True" Visibility="Collapsed"/> <DatePicker x:Name="DateCondition" Text="{Binding DateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="True" Visibility="Collapsed"/> </Grid> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding ValueType}" Value="Text"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Visible"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Hidden"/> </DataTrigger> <DataTrigger Binding="{Binding ValueType}" Value="Combobox"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Visible"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Hidden"/> </DataTrigger> <DataTrigger Binding="{Binding ValueType}" Value="Date"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding IsModified, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="Background" TargetName="TextBoxCondition" Value="Cornsilk"/> <Setter Property="Background" TargetName="ComboBoxCondition" Value="Cornsilk"/> <Setter Property="Background" TargetName="DateCondition" Value="Cornsilk"/> </DataTrigger> <DataTrigger Binding="{Binding IsModified, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="False"> <Setter Property="Background" TargetName="TextBoxCondition" Value="White"/> <Setter Property="Background" TargetName="ComboBoxCondition" Value="White"/> <Setter Property="Background" TargetName="DateCondition" Value="White"/> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 如果 DataView 基于 List<object> gridAttributes = new List<AttributeItem>(); gridAttributes add some data ..... DataView = CollectionViewSource.GetDefaultView(gridAttributes); 一切都按预期进行。在 DataGridCell 中,我有 AttributeIem 对象,并且可以根据需要设置其样式。 但是,当 DataView 基于 DataTable 时,然后在 DataGridCell 中,由于某种原因我得到了 DataRowView,然后 Style 不起作用,一切都出错了。 RunDataTable.Rows.Clear(); RunDataTable.Columns.Clear(); RunDataTable.Columns.Add("SectionName"); RunDataTable.Columns.Add("RunName"); foreach (AttributeItem attr in gridAttributes) { RunDataTable.Columns.Add(attr.AttributeName, typeof(AttributeItem)); } DataRow row = RunDataTable.NewRow(); row["SectionName"] = section; row["RunName"] = name; foreach (AttributeItem item in gridAttributes) { row[item.AttributeName] = item; } RunDataTable.Rows.Add(row); DataView = new DataView(RunDataTable); 这是我收到的错误绑定列表 System.Windows.Data Error: 40 : BindingExpression path error: 'IsModified' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=IsModified; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'Values' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=Values; DataItem='DataRowView' (HashCode=47477033); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=SelectedValue; DataItem='DataRowView' (HashCode=47477033); target element is 'ComboBox' (Name=''); target property is 'SelectedValue' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'TextValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=TextValue; DataItem='DataRowView' (HashCode=47477033); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'DateValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=DateValue; DataItem='DataRowView' (HashCode=47477033); target element is 'DatePicker' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'ValueType' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=ValueType; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'ValueType' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=ValueType; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'ValueType' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=ValueType; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'IsModified' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=IsModified; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'IsModified' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=IsModified; DataItem='DataRowView' (HashCode=47477033); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'Values' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=Values; DataItem='DataRowView' (HashCode=47477033); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=SelectedValue; DataItem='DataRowView' (HashCode=47477033); target element is 'ComboBox' (Name=''); target property is 'SelectedValue' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'TextValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=TextValue; DataItem='DataRowView' (HashCode=47477033); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'DateValue' property not found on 'object' ''DataRowView' (HashCode=47477033)'. BindingExpression:Path=DateValue; DataItem='DataRowView' (HashCode=47477033); target element is 'DatePicker' (Name=''); target property is 'Text' (type 'String') 任何建议我如何调整我的样式或代码隐藏以使其正常工作。 谢谢。 我尝试了不同的数据源方式。但不幸的是每次我都会遇到同样的错误 添加列: public static void RefreshColumnsFromModel(this DataGrid datagrid, DataGridRuns gridModel) { var myResourceDictionary = new ResourceDictionary(); myResourceDictionary.Source = new Uri("/SomeSource;component/Resources/SomeSource.xaml", UriKind.RelativeOrAbsolute); datagrid.Columns.Clear(); foreach (ColumnWithFilter column in gridModel.Columns.OrderBy(c => c.Index)) { if (column.IsReadOnly) { datagrid.Columns.Add( new DataGridTextColumn() { Header = column.ColumnHeader, IsReadOnly = column.IsReadOnly, Binding = new Binding(column.ColumnBinding), CellStyle = myResourceDictionary["DataGridReadOnlyCellStyle"] as Style, HeaderTemplate = datagrid.Resources["HeaderTemplate"] as DataTemplate } ); } else { datagrid.Columns.Add( new DataGridTemplateColumn() { Header = column.ColumnHeader, IsReadOnly = column.IsReadOnly, //Binding = new Binding(column.ColumnBinding), //HeaderTemplate = myResourceDictionary["HeaderTemplate"] as DataTemplate HeaderTemplate = datagrid.Resources["HeaderTemplate"] as DataTemplate, CellStyle = myResourceDictionary["DataGridDataViewCustomCellStyle"] as Style } ); } } } DataGridRuns 代表 DataGrid 的模型,一些用于过滤和排序的自定义行为 public class DataGridRuns : BaseClass { private List<AttributeItem> gridAttributes = new List<AttributeItem>(); public ObservableCollection<AttributeItem> GridAttributes { get { return new ObservableCollection<AttributeItem>(gridAttributes); } set { gridAttributes = value.ToList(); RaisePropertyChanged(); } } public DataTable RunDataTable { get; set; } = new DataTable("RunData"); private DataView dataView = null; public DataView DataView { get { return dataView; } } private ColumnWithFilter selectedColumn = null; public ColumnWithFilter SelectedColumn { get { return selectedColumn; } set { selectedColumn = value; RaisePropertyChanged(); } } public Action RefreshColumns; private List<ColumnWithFilter> columns = new List<ColumnWithFilter>(); public ObservableCollection<ColumnWithFilter> Columns { get { return new ObservableCollection<ColumnWithFilter>(columns); } set { columns = value.ToList(); RaisePropertyChanged(); } } public DataGridRuns() { } private List<AttributeItem> CreateAttributesFromInfo(IList<AttributeInfo> _attributes) { List<AttributeItem> result = new List<AttributeItem>(); try { foreach (var attr in _attributes) { result.Add(new AttributeItem(attr)); } } catch (Exception ex) { } return result; } public void AddRun(string section, string name, IList<AttributeInfo> _attributes, bool template = false) { var attr = CreateAttributesFromInfo(_attributes); if (gridAttributes == null || gridAttributes.Count() == 0) { gridAttributes = new List<AttributeItem>(attr); CreateColumns(); } DataRow row = RunDataTable.NewRow(); row["SectionName"] = section; row["RunName"] = name; foreach (AttributeItem item in gridAttributes) { row[item.AttributeName] = item; } RunDataTable.Rows.Add(row); dataView = new DataView(RunDataTable); //dataView = CollectionViewSource.GetDefaultView(RunDataTable); } public void ClearData() { gridAttributes.Clear(); gridAttributes = new List<AttributeItem>(); } public void CreateColumns() { RunDataTable.Rows.Clear(); RunDataTable.Columns.Clear(); columns = new List<ColumnWithFilter>(); ColumnWithFilter col = null; col = new ColumnWithFilter("Section Name", nameof(AttributeItem.SectionName)) { Index = 0, IsReadOnly = true, }; columns.Add(col); RunDataTable.Columns.Add("SectionName"); col = new ColumnWithFilter("Run Name", nameof(AttributeItem.RunName)) { Index = 1, IsReadOnly = true, }; columns.Add(col); RunDataTable.Columns.Add("RunName"); int colIndex = 1; foreach (AttributeItem attr in gridAttributes) { col = new ColumnWithFilter(attr.AttributeName, nameof(attr)) { Index = ++colIndex, IsReadOnly = false, }; columns.Add(col); RunDataTable.Columns.Add(attr.AttributeName, typeof(AttributeItem)); } } public void RefreshDataView() { foreach (DataRow item in RunDataTable.Rows) { item.ItemArray.ToList().ForEach(a => (a as AttributeItem)?.StartMarkingModifications()); } RaisePropertyChanged(nameof(DataView)); } public void RefreshPopupContent(string header = "") { selectedColumn = columns.Where(c => c.ColumnHeader.Equals(header)).FirstOrDefault(); if (selectedColumn == null) return; selectedColumn.RefreshFilterValues(gridAttributes .Where(a => !string.IsNullOrEmpty((string)GetPropertyValue(a, selectedColumn.ColumnBinding))) .Select(a => (string)GetPropertyValue(a, selectedColumn.ColumnBinding)).Distinct().ToList()); RaisePropertyChanged(nameof(SelectedColumn)); } public void Filtering() { UpdateDataViewRowFilter(); } private void UpdateDataViewRowFilter() { string rowFilter = string.Empty; foreach (ColumnWithFilter column in columns.Where(c => c.FilterIndex > 0)) { string andStr = !string.IsNullOrEmpty(rowFilter) ? " AND " : ""; switch (column.FilterIndex) { case 1: rowFilter += andStr + $"{column.ColumnBinding} IS NULL"; break; default: rowFilter += andStr + $"{column.ColumnBinding} = '{column.FilteredValue}'"; //filterResult = ((string)GetPropertyValue(attribute, column.ColumnBinding)).Equals(column.FilteredValue); break; } } (dataView as DataView).RowFilter = rowFilter; } public object GetPropertyValue(object obj, string propertyName) { return obj.GetType().GetProperty(propertyName)?.GetValue(obj, null); } } 这是我在 DataTable 中看到的,我希望在 DataGrid 中也能看到同样的结果 DataTable Visualizer 的屏幕截图 添加这是我在 UI 中看到的 具有列过滤的自定义网格 我不确定我是否正确理解了您的所有代码。我会根据我能理解的提供一个解决方案。 在数据表单元格中,您的类型为“AttributeItem”。具有路径“column.ColumnBinding”的绑定将从相应的单元格返回该类型的实例。 ValueType 和 IsModified 属性(在 ControlTemplate 中使用)属于此实例。 在这种情况下,我认为您错误地使用了 DataGridCell 样式更改。相反,您需要为单元格的内容创建一个数据模板。 <DataTemplate x:Key="DataGridDataViewCustomCellDataTemplate"> <Grid> <ComboBox x:Name="ComboBoxCondition" IsEditable="True" ItemsSource="{Binding Values}" Visibility="Collapsed" SelectedValue="{Binding SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ValueID" DisplayMemberPath="ValueStr"/> <TextBox x:Name="TextBoxCondition" Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="True" Visibility="Collapsed"/> <DatePicker x:Name="DateCondition" Text="{Binding DateValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="True" Visibility="Collapsed"/> </Grid> <DataTemplate.Triggers> <DataTrigger Binding="{Binding ValueType}" Value="Text"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Visible"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Hidden"/> </DataTrigger> <DataTrigger Binding="{Binding ValueType}" Value="Combobox"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Visible"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Hidden"/> </DataTrigger> <DataTrigger Binding="{Binding ValueType}" Value="Date"> <Setter Property="Visibility" TargetName="TextBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="ComboBoxCondition" Value="Hidden"/> <Setter Property="Visibility" TargetName="DateCondition" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding IsModified, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="True"> <Setter Property="Background" TargetName="TextBoxCondition" Value="Cornsilk"/> <Setter Property="Background" TargetName="ComboBoxCondition" Value="Cornsilk"/> <Setter Property="Background" TargetName="DateCondition" Value="Cornsilk"/> </DataTrigger> <DataTrigger Binding="{Binding IsModified, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="False"> <Setter Property="Background" TargetName="TextBoxCondition" Value="White"/> <Setter Property="Background" TargetName="ComboBoxCondition" Value="White"/> <Setter Property="Background" TargetName="DateCondition" Value="White"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> 在模板 DataGridTemplateColumn 中,您需要创建一个带有 ContentControl 的模板,该模板将接受 Content 属性中路径“column.ColumnBinding”的绑定以及上述 ContentTemplate 属性中的模板。 private const string templateString = @" <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <ContentControl Content=""{{Binding {0}}}"" ContentTemplate=""{{DynamicResource DataGridDataViewCustomCellDataTemplate}}""/> </DataTemplate> "; public static void RefreshColumnsFromModel(this DataGrid datagrid, DataGridRuns gridModel) { var myResourceDictionary = new ResourceDictionary(); myResourceDictionary.Source = new Uri("/SomeSource;component/Resources/SomeSource.xaml", UriKind.RelativeOrAbsolute); datagrid.Columns.Clear(); foreach (ColumnWithFilter column in gridModel.Columns.OrderBy(c => c.Index)) { if (column.IsReadOnly) { datagrid.Columns.Add( new DataGridTextColumn() { Header = column.ColumnHeader, IsReadOnly = column.IsReadOnly, Binding = new Binding(column.ColumnBinding), CellStyle = myResourceDictionary["DataGridReadOnlyCellStyle"] as Style, HeaderTemplate = datagrid.Resources["HeaderTemplate"] as DataTemplate } ); } else { string templateSource = string.Format(templateString, column.ColumnBinding); var template = XamlReader.Parse(templateSource); datagrid.Columns.Add( new DataGridTemplateColumn() { Header = column.ColumnHeader, IsReadOnly = column.IsReadOnly, //Binding = new Binding(column.ColumnBinding), //HeaderTemplate = myResourceDictionary["HeaderTemplate"] as DataTemplate HeaderTemplate = datagrid.Resources["HeaderTemplate"] as DataTemplate, CellStyle = myResourceDictionary["DataGridDataViewCustomCellStyle"] as Style } ); } } } 不幸的是,我无法测试这段代码。但如果您提供重现这些情况的最少代码,我可以进行测试并准备有保证的工作版本。