繁体   English   中英

该视图不允许使用“ EditItem”。 尝试在DataGrid中编辑项目时

[英]''EditItem' is not allowed for this view.' when trying to edit a item in the DataGrid

因此,当我双击一个项目以编辑我的数据网格中的值时,我总是收到错误消息。

该视图不允许使用“ EditItem”。

它看起来像这样 在此处输入图片说明

我之前从未遇到过,所以我不确定该如何处理。 是什么导致此问题,以及解决此问题的正确方法是什么,因此我知道将来如何处理。 我尝试在Google上进行调查,但都需要执行列表操作,并且由于我没有使用列表,所以无法看到与应用程序的连接。

XAML

    <DataGrid Name="dgItems">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Property" Binding="{Binding Property}" />
            <DataGridTextColumn Header="Value" Binding="{Binding Value}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

CS

private void btnStart_Click(object sender, RoutedEventArgs e)
{
    string path = "";
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Properties | *.properties";
    if (ofd.ShowDialog() == true)
    {
        path = ofd.FileName;
    }
    using (StreamReader sr = new StreamReader(path))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (!line.StartsWith("#"))
            {
                string[] lines = line.Split('=');
                string property = lines[0];
                string value = lines[1];
                this.dgItems.Items.Add(new ServerProperties { Property = property, Value = value });
                Debug.Print($"Property: {property} Value: {value}");
            }
        }
    }
}

获取并设置值的我的班级

public class ServerProperties
{
    public string Property { get; set; }
    public string Value { get; set; }
}

您应该将DataGridItemsSource属性设置为一个实现IList接口的集合,以便您可以编辑项目:

var list = new List<ServerProperties> { ... };
dgItems.ItemsSource = list;

不要将任何项目直接添加到DataGridItems属性中:

dgItems.Items.Add(new ServerProperties());

因此,您应该稍微修改代码:

private void btnStart_Click(object sender, RoutedEventArgs e)
{
    string path = "";
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Properties | *.properties";
    if (ofd.ShowDialog() == true)
    {
        path = ofd.FileName;
    }
    List<ServerProperties> serverProperties = new List<ServerProperties>();
    using (StreamReader sr = new StreamReader(path))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            if (!line.StartsWith("#"))
            {
                string[] lines = line.Split('=');
                string property = lines[0];
                string value = lines[1];
                serverProperties.Add(new ServerProperties { Property = property, Value = value });
                Debug.Print($"Property: {property} Value: {value}");
            }
        }
    }
    dgItems.ItemsSource = serverProperties;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM