用户无法在绑定到observablecollection的datagrid上添加行 [英] user cannot add row on datagrid bound to observablecollection

查看:107
本文介绍了用户无法在绑定到observablecollection的datagrid上添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能说出为什么这个数据网格不显示新的一行?

公共ObservableCollection< string> PasswordGuesses {get {return _passwordGuesses ?? (_passwordGuesses = new ObservableCollection< string>(){"password"}); }}

Can anyone tell why this datagrid doesn''t show the new row, row?

public ObservableCollection<string> PasswordGuesses { get { return _passwordGuesses ?? (_passwordGuesses = new ObservableCollection<string>() { "password" }); } }

<DataGrid Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding PasswordGuesses}" CanUserAddRows="True">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Password" Binding="{Binding .}" MinWidth="150"/>
        <!--<DataGridTemplateColumn Header="Operations">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="-" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.DeletePasswordCommand}" CommandParameter="{Binding .}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>-->
    </DataGrid.Columns>
</DataGrid>

推荐答案

您需要将AutoGenerateColumns 设置为true 或提供DataGridTextColumn可以绑定到的属性.在后一种情况下,您将需要定义一个具有一个属性的模型,创建该模型的实例,将其添加到集合中并显示它们.
You either need to set AutoGenerateColumns to true or provide a property that the DataGridTextColumn can bind to. In the latter case, you will need to define a model with one property, create instance of that model, add them to the collection and display them.


这篇关于用户无法在绑定到observablecollection的datagrid上添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆