gpt4 book ai didi

wpf - Datagrid/WPF - 枚举类型的奇怪显示行为

转载 作者:行者123 更新时间:2023-12-02 00:27:31 25 4
gpt4 key购买 nike

问题

当我尝试在 WPF 数据网格中显示枚举类型时,自动分配的组合框未正确显示。

当数据网格首次显示时,“enum”根本不显示,而其他类型如“bool”和“double”按预期显示:

Before editing

但是如果我点击枚举单元格,我会看到组合框出现:

After editing

如何让组合框立即出现?

定义

在我的确切情况下,我只想显示 PathSettingsList

PathSetting 就是这样定义的一堆数据:

public class PathSettings
{
public bool IsSelected { get; set; }
public PathType PathType { get; set; }
public double Gain { get; set; }
}

其中 PathType 是一些枚举值:

public enum PathType
{
Direct,
Amplified,
Load
}

假设我想显示以下列表:

var tests = new List<PathSettings>
{
new PathSettings { IsSelected = false, PathType = PathType.Direct, Gain = 2.0 },
new PathSettings { IsSelected = true, PathType = PathType.Amplified, Gain = 2.5 },
new PathSettings { IsSelected = false, PathType = PathType.Load, Gain = 0.9 },
};

编码尝试

我首先创建了一个 ViewAdapter 类来将我的 List 转换为一些 DataView 对象:

public class ViewAdapter
{
private static IEnumerable<PathSettings> buildDummyEntries()
{
return new List<PathSettings>
{
new PathSettings { IsSelected = false, PathType = PathType.Direct, Gain = 2.0 },
new PathSettings { IsSelected = true, PathType = PathType.Amplified, Gain = 2.5 },
new PathSettings { IsSelected = false, PathType = PathType.Load, Gain = 0.9 },
};
}

public ViewAdapter() : this(buildDummyEntries())
{

}
public ViewAdapter(IEnumerable<PathSettings> settings)
{
if (settings == null) { throw new ArgumentNullException(); }

// 1) Transform to cell collection
var cells = new ObservableCollection<ObservableCollection<object>>();
foreach (var s in settings)
{
cells.Add(new ObservableCollection<object>
{
s.IsSelected,
s.PathType,
s.Gain,
});
}

// 2) Transform to datatable
var dataTable = new DataTable();
dataTable.Columns.Add("IsSelected", typeof(bool));
dataTable.Columns.Add("PathType", typeof(PathType));
dataTable.Columns.Add("Gain", typeof(double));
foreach (var t in settings)
{
dataTable.Rows.Add(dataTable.NewRow());
}

// 3) Transform to data view and feed with cells
Settings = new DataView(dataTable);
for (var i = 0; i < settings.Count(); i++)
{
for (var j = 0; j < cells[i].Count; j++)
{
Settings[i][j] = cells[i][j];
}
}
}

public DataView Settings
{
get;
private set;
}
}

并用它喂养我的 DataGridItemsSource:

<Window x:Class="EnumInDataGrid.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EnumInDataGrid"

Title="MainWindow"

mc:Ignorable="d"
d:Height="350" d:Width="525"
d:DataContext="{d:DesignInstance Type=local:ViewAdapter, IsDesignTimeCreatable=True}"
d:DesignHeight="239" d:DesignWidth="308">

<Window.DataContext>
<local:ViewAdapter />
</Window.DataContext>

<Grid>
<DataGrid
ItemsSource="{Binding Settings}"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
SelectionMode="Single"
SelectionUnit="Cell"/>
</Grid>

</Window>

最佳答案

默认情况下,WPF DataGrids 处于只读模式,您必须单击一个单元格才能对其进行编辑。 ComboBox 列的只读模板是 TextBlock,而编辑模板是 ComboBox。

如果您想立即显示 ComboBox,请使用自定义 DataGridTemplateColumn包含一个组合框。

至于枚举值没有立即显示,我怀疑它与您的数据源有关。我做了一个快速测试,包含枚举值的自动生成的列确实正确显示。

确定数据源是否有问题的一种方法是去掉 DataView。 DataGrid 可以绑定(bind)到任何可枚举的集合,因此将它直接绑定(bind)到您的 List<PathSettings>而不是创建 DataView .

我真的不知道你为什么要使用 DataView对象放在首位。 WPF 中的理想集合应该是 ObservableCollection<DataObject>DataObject应该是实现 INotifyPropertyChanged 的东西.这可确保在您的集合或属性更改时自动更新 UI。

关于wpf - Datagrid/WPF - 枚举类型的奇怪显示行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8314652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com