Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datagrid/WPF-枚举类型的奇怪显示行为 问题_Wpf_Wpfdatagrid - Fatal编程技术网

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

Datagrid/WPF-枚举类型的奇怪显示行为 问题,wpf,wpfdatagrid,Wpf,Wpfdatagrid,当我试图在WPF数据网格中显示枚举类型时,自动分配的组合框不会正确显示 首次显示数据网格时,完全不显示“enum”,而其他类型(如“bool”和“double”)则按预期显示: 但如果单击枚举单元格,我会看到组合框出现: 如何使组合框立即出现 定义 在我的具体情况下,我只想显示一个列表,其中包含路径设置 PathSetting就是这样定义的一组数据: public class PathSettings { public bool IsSelected { get; set; }

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

首次显示数据网格时,完全不显示“enum”,而其他类型(如“bool”和“double”)则按预期显示:

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

如何使组合框立即出现

定义 在我的具体情况下,我只想显示一个
列表,其中包含
路径设置

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 },
};
并将其输入my
DataGrid的
ItemsSource

<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数据网格处于只读模式,您必须单击单元格来编辑它们。ComboBox列的只读模板是TextBlock,而编辑模板是ComboBox

如果要立即显示组合框,请使用包含组合框的自定义
DataGridTemplateColumn

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

确定数据源是否存在问题的一种方法是删除DataView。DataGrid可以绑定到任何可枚举集合,因此直接将其绑定到您的
列表
,而不是创建
数据视图


我真的不知道为什么要首先使用
DataView
对象。理想情况下,WPF中的集合应该是
ObservableCollection
DataObject
应该是实现
INotifyPropertyChanged
的集合。这确保了当集合或属性发生更改时,UI会自动更新。

您有自定义网格样式吗?是否使用AutoGenerateColumns?@sll,没有,上面提供了几乎所有的代码,您可以在Visual Studio中的一个简单的新WPF应用程序项目中围绕它来重现相同的问题。@MBen,我不修改AutoGenerateColumns属性,因此它必须保留默认值。非常感谢,直接绑定到路径设置列表解决了这个问题。我首先使用DataView的原因是我对WPF不熟悉,我刚刚复制粘贴了我看到的一些示例。起初,我对从集合中更新UI不太感兴趣(同时担心如何做到这一点),但这是关于如何做到这一点的非常有价值的指导。再次感谢。@citizensane没问题,很高兴能解决:)另外,如果您是WPF新手,我强烈建议您研究MVVM设计模式。它非常适合WPF的绑定系统,并将UI与业务逻辑分开。如果您感兴趣,我在这里写了一个简短的介绍:
<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>