datagrid 相关问题

一个图形用户界面元素,用于显示数据的表格视图。

C#、WPF、将 List<string> 绑定到 DataGrid

我无法将列表绑定到 DataGrid。它应该尽可能简单。我是 WPF 新手,这是为了我的个人教育。 我有一个视图(编辑器)、视图模型(VMText)和数据(JustText)...

回答 3 投票 0

数据网格 WPF 中的单元格验证基于其他行中的单元格值

我想创建一个简单的时间表应用程序,其中用户可以拥有不同的角色,并且根据角色他可以更新和查看时间表。为了更新时间表,我实施了 da...

回答 1 投票 0

WPF DataGrid ScrollIntoView 不会滚动到请求的行

我正在使用 WPF DataGrid,它有大约 50 行。我正在使用 MVVM 模式。 用户可以搜索一行中的某些文本,数据网格应突出显示该行(不一定选择该行)并滚动到该行...

回答 3 投票 0

Material-UI DataGrid:我们可以展开折叠Material-UI DataGrid中的每一行吗

是否可以使用Material-UI DataGrid为每行实现展开折叠? 我知道我们应该能够使用 TableRow 来完成此操作并手动渲染它(可折叠表格),但是它是...

回答 3 投票 0

WPF DataGrid - 如何从 ViewModel 中选择位置 [0,0] 的单元格

我有一个SelectionMode =“Single”SelectionUnit =“Cell”的DataGrid。 目前,当显示 DataGrid 时,最初不会选择任何单元格。 我有两个场景需要解决...

回答 1 投票 0

在 MUI 数据网格中强制水平滚动

有没有办法强制水平滚动条始终出现在 MUI 数据网格中?我有一个包含数十列的表,我们需要能够指示还有更多列可供查看...

回答 4 投票 0

根据数据表错误设置wpf Datagrid单元格背景

我有一个数据网格 我有一个数据网格 <DataGrid x:Name="myDataGrid" HorizontalAlignment="Left" Height="304" Margin="0,122,0,0" VerticalAlignment="Top" Width="790" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SelectionUnit="CellOrRowHeader" Style="{DynamicResource DataGridStyle1}" CanUserSortColumns="False"> </DataGrid> 并在代码中的某处设置其内容,其中 p_dtbl 是加载了 SQL 的数据表。 (所以我现在不知道它可能包含哪些列) Me.myDataGrid.ItemsSource = Me.p_dtbl.DefaultView 处理事件时(当用户输入数据时),我设置了数据表的 colError 。 CType(Me.myDataGrid.CurrentItem, DataRowView).Row.SetColumnError(strColName, strErrorMsg) 如果 GridView 单元格相应的数据表单元格有错误,如何将其背景颜色设置为红色? (没有错误时返回白色) 错误消息的工具提示文本? 您可以使用一个转换器来调用 GetColumnError 的 DataRow 方法: public class ErrorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DataRow row = value as DataRow; string columnName = parameter as string; return row != null && !string.IsNullOrEmpty(columnName) && !string.IsNullOrEmpty(row.GetColumnError(columnName)); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } 然后处理 AutoGeneratingColumn 为每列定义自定义 CellTemplate: myDataGrid.AutoGeneratingColumn += (s, e) => { string CellStyle = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" + " xmlns:local=\"clr-namespace:WpfApp1;assembly=WpfApp1\" TargetType=\"DataGridCell\">" + "<Style.Triggers>" + "<DataTrigger Value=\"True\">" + "<DataTrigger.Binding>" + $"<Binding Path=\"Row\" ConverterParameter=\"{e.PropertyName}\">" + "<Binding.Converter>" + "<local:ErrorConverter />" + "</Binding.Converter>" + "</Binding>" + "</DataTrigger.Binding>" + "<Setter Property=\"Background\" Value=\"Yellow\"/>" + "</DataTrigger>" + "</Style.Triggers>" + "</Style>"; e.Column.CellStyle = XamlReader.Parse(CellStyle) as Style; }; 记住将 WpfApp1 更改为程序集和命名空间的名称。

回答 1 投票 0

使用 Blazor 服务器应用程序中的资源文件进行 MudBlazor 数据网格本地化

我正在尝试使用资源文件本地化 MudBlazor 数据网格。但数据网格过滤项仍以英文显示。 据此我添加了以下课程: 使用 Microsoft.Extensions。

回答 1 投票 0

无法创建带有按钮和单击处理程序的未绑定列

我有一个数据网格,我正在根据从电子表格中读取的列动态构建该数据网格,该电子表格可以具有可变的列数。当我只有文本或枚举时,一切都工作正常......

回答 1 投票 0

具有多重选择的MudDataGrid不能以两种方式工作,@bind-SelectedItems =“selectedRoles”

我有这个网格: @if (集合!= null && collections.Roles!= null && collections.Roles.Count() > 0 && selectedRoles != null) { 我有这个网格: @if (collections != null && collections.Roles != null && collections.Roles.Count() > 0 && selectedRoles != null) { <MudBlazor.MudDataGrid T="Role" MultiSelection="true" Items="@collections.Roles" @bind-SelectedItems="selectedRoles"> <Columns> <PropertyColumn Property="x => x.Name" Title="Caption" Style="width: 100px;" /> <MudBlazor.SelectColumn T="Role" ShowInFooter="false" /> </Columns> </MudBlazor.MudDataGrid> } 当我选择一些项目并单击“提交”时,网格起作用,正确发送选定的项目。 但是当页面首次加载时我会这样做: HashSet<Role> selectedRoles = null; protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); await GetPerson(); selectedRoles = person.User.Roles.ToHashSet<Role>(); StateHasChanged(); } 但是,当页面首次加载时,数据网格中没有选择任何角色,并且 person.User.Roles.ToHashSet<Role>(); 代码中存在数据网格未显示的项目。 我在 oninitialized 方法中使用此代码解决了我的问题: selectedRoles = person.User.Roles.Select(r => collections.Roles.First(r2 => r2.Id == r.Id)).ToHashSet<Role>(); 角色必须是同一被选择的对象。

回答 1 投票 0

过滤绑定的数据网格,使其仅显示具有特定值的项目(Winforms)

因此,在我的 winforms 项目中,我有一个绑定到“摔跤手”类的数据网格。这个类有许多不同的属性,例如“姓名”,“性别”,“头衔”(该人是否有冠军......

回答 1 投票 0

DataGrid 更改背景颜色更改多行颜色

我正在尝试更改数据网格的行或单元格的背景颜色,对我来说,我得到了一些奇怪的变化。 当我更改 X 行或单元格的颜色时,我的列表中的另一行也会...

回答 1 投票 0

DataGrid 上的 ScrollBar 不断跳回顶部

我正在使用 WPF MVVM 解决方案,其中我有一个每秒更新多次的网格。我需要允许用户能够向下滚动网格一半并保持在那里......

回答 3 投票 0

WPF 选择 DataGrid 中的所有复选框

我试图选择 DataGrid 中的所有复选框,但使用下面的代码没有得到任何结果 这是我在单击主复选框时调用的函数 private void CheckUnCheck...

回答 4 投票 0

WPF DataGrid 不使用 DataTemplate 和 DataTriggers 显示数据

我有ViewModel 公共类视图模型 { 公共字符串测试字段{获取;放; } 公共 ObservableCollection TestCollection { 获取;放; } 公共视图模型() { ... 我有视图模型 public class ViewModel { public string TestField { get; set; } public ObservableCollection<object> TestCollection { get; set; } public ViewModel() { TestField = "ABCD"; } } 我想根据 TestField 的值使用不同的 DataGridTemplateColumns。 DataGrid 将 TestCollection 作为其 ItemSource,并且在 DataTemplate 中,我想使用 ItemSource 中的项目。不过DataTemplate的选择需要依赖于TestField。 问题在于 TestField 位于 TestCollection 的上下文之外。 一种解决方案是使用转换器将 TestField 属性绑定到 CellTemplate 属性。 如果 TestField 属性在创建 ViewModel 实例后可以更改,则需要通过 INotifyPropertyChanged.PropertyChanged 事件提供其更改的通知。在我的示例中,这是通过 ViewModelBase 基类的方法完成的: public class TestFieldViewModel : ViewModelBase { public string TestField { get => Get<string>(); set => Set(value); } public ObservableCollection<object> TestCollection { get; /*set;*/ } = new(); public TestFieldViewModel() { foreach (var item in "Example") { TestCollection.Add(item); } // асинхронная смена значения с целью тестирования Task.Run(async () => { while (true) { await Task.Delay(2000); TestField = TestField == "ABCD" ? string.Empty : "ABCD"; } }); } } public class TestFieldTemplateConverter : IValueConverter { public DataTemplate Default { get; set; } public DataTemplate ABCD { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value switch { "ABCD" => ABCD, _ => Default }; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } <Window ---------------------------- ---------------------------- DataContext="{DynamicResource vm}"> <Window.Resources> <local:TestFieldViewModel x:Key="vm"/> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding TestCollection}"> <DataGrid.Resources> <DataTemplate x:Key="default"> <TextBlock Text="Default"/> </DataTemplate> <DataTemplate x:Key="ABCD"> <TextBlock Text="ABCD"/> </DataTemplate> <local:TestFieldTemplateConverter x:Key="templateConverter" Default="{StaticResource default}" ABCD="{StaticResource ABCD}"/> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn CellTemplate="{Binding TestField, Source={StaticResource vm}, Converter={StaticResource templateConverter}}"> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>

回答 1 投票 0

使我的数据对象适合(轻松)与 DataGridView 和/或 DataGrid 一起使用

我继承了一个符合IEnumerable的模型对象,但没有继承其他Binding相关的接口。该对象类似于表格,通常填充最终来自......的数据

回答 1 投票 0

C# WPF DataGrid 更改值 PropertyChange 的行格式(例如背景)

我的目标是当底层值属性更改时更改 DataGrid 的行/单元格样式。它与值无关,DataGrid 应该只显示它已更改。我有一个清单

回答 1 投票 0

滚动 DataGrid 时出现 InvalidOperationException:绑定表达式已属于 BindingGroup

当尝试在 DataGrid 上向下滚动时,我收到 InvalidOperationException,指出“绑定表达式已经属于 BindingGroup;无法将其添加到不同的 BindingGrou...

回答 2 投票 0

使用上下文菜单时的数据网格行背景颜色

请帮助我 当我使用行数据网格的上下文菜单时,背景会更改为不同的颜色。 如何修复它? 标准背景 使用上下文菜单时的背景 请帮助我 当我使用行数据网格的上下文菜单时,背景会更改为不同的颜色。 如何修复它? <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Grid.Row="1" CanUserAddRows="False" ItemsSource="{Binding Model.FlatModel}" IsReadOnly="True" GridLinesVisibility="None" HeadersVisibility="Column" SelectionUnit="FullRow" CanUserSortColumns="False" RowHeight="20" > <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> </Style> </DataGrid.CellStyle> <DataGrid.RowStyle> <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}"> <Setter Property="ContextMenu" Value="{StaticResource cm}"/> </Style> </DataGrid.RowStyle> <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#A6D8FF"/> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </DataGrid.Resources> <DataGrid.Columns> </DataGrid.Columns> </DataGrid> 如果这个上下文菜单,即使同时选择多个项目也能很好地工作。 <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Foreground" Value="Black"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="True"/> <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=ContextMenu.IsOpen}" Value="True"/> </MultiDataTrigger.Conditions> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </MultiDataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle>

回答 1 投票 0

MVVM - 在 .Clear 和 new 加载 ObservableCollection 之后保留选择

我有一个应用程序,它使用 SQL Select 提取 DataTable,然后将每个项目加载到每个类的 ObservableCollection 中。 视图模型: 公共可观察集合 我有一个应用程序,它使用 SQL Select 提取 DataTable,然后将每个项目加载到每个类的 ObservableCollection 中。 视图模型: public ObservableCollection<SchachtelEintrag> DataGridMainCollection { get; set; } = new ObservableCollection<SchachtelEintrag>(); private void loadDataGridMainCollection() { DataGridMainCollection.Clear(); DataTable dt = new DataTable(); dt = mySql.selectSql("SELECT id, datum, artikel, kurztext, stueckzahl, blechstaerke, rohrmasse, " + "material, kundenauftrag, ansprechpartner, abteilung, geaendertAm, twoWeeks, prio, ordnerpfad, dateiname FROM auftragstool.orders WHERE zustand = 'zu schachteln' ORDER BY import ASC, blechstaerke DESC"); if (dt.Rows.Count > 0) { foreach (DataRow r in dt.Rows) { DataGridMainCollection.Add(new SchachtelEintrag { Id = Convert.ToInt32(r["id"].ToString()), Datum = Convert.ToDateTime(r["datum"].ToString()), Artikel = r["artikel"].ToString(), Kurztext = r["kurztext"].ToString(), Stueckzahl = Convert.ToInt32(r["stueckzahl"].ToString()), Blechstaerke = r["blechstaerke"].ToString(), Rohrmasse = r["rohrmasse"].ToString(), Material = r["material"].ToString(), Kundenauftrag = r["kundenauftrag"].ToString(), Ansprechpartner = r["ansprechpartner"].ToString(), Abteilung = r["abteilung"].ToString(), GeaendertAm = r["geaendertAm"].ToString(), TwoWeeks = r["twoWeeks"].ToString(), Prio = r["prio"].ToString(), Ordnerpfad = r["ordnerpfad"].ToString(), Dateiname = r["dateiname"].ToString() }); } } } 我的 DataGrid 绑定到 ObservableCollection。 XAML: <DataGrid ItemsSource="{Binding DataGridMainCollection}" Selected="{Binding SelectedRow, Mode=TwoWay}"> 现在不同的人正在使用该程序。一旦位置被删除或添加,程序就会向 TCP 服务器发送一个字符串。 TCP 服务器将命令发送回客户端,从而重新触发以下代码: 视图模型: loadDataGridMainCollection() //Void from above 现在我知道重建 ObservableCollection 后,网格中的 SelectedItem 不再可用于选择项目。 其他人如何处理这个问题以更新整个集合而不失去焦点? 在清除集合之前,请备份当前选定的项目(或仅备份选定项目的 ID)。清除并刷新后,您必须在新填充的列表中找到您的备份项目。 这可以通过以下方式完成: int? currentlySelectedId = null; if(SelectedRow != null) currentlySelectedId = SelectedRow.Id; DataGridMainCollection.Clear(); // your code to refresh the data here if(currentlySelectedId != null) SelectedRow = DataGridMainCollection.FirstOrDefault(i => i.Id == backupItem.Id);

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.