简体   繁体   English

WPF 数据网格列的默认值

[英]Default Value For WPF Datagrid Column

I have a DataGrid in my WPF application.我的 WPF 应用程序中有一个 DataGrid。 I was wondering if there is a way to show default values for a column in the datagrid.我想知道是否有办法显示数据网格中列的默认值。 For example, I let the user have the ability to add new rows to the DataGrid.例如,我让用户能够向 DataGrid 添加新行。 Hence, at the bottom of the DataGrid, there is always a blank, empty row for the user to add data to.因此,在 DataGrid 的底部,总是有一个空白的空行供用户添加数据。 I was hoping to have a default value for a column in this row.我希望这一行中的列有一个默认值。 Is there any way to achieve this functionality?有什么办法可以实现这个功能吗? Thanks!谢谢!

Let's say ItemsSource of your DataGrid is a Collection of SomeClass and you are displaying different Properties of SomeClass in DataGrid Columns.假设您的DataGridItemsSourceSomeClass的集合,并且您在 DataGrid 列中显示SomeClass不同Properties What you can do is in constructor of default SomeClass assign the Properties some default value.您可以做的是在默认SomeClass构造函数中为Properties分配一些默认值。 In this way you can have default column value in DataGrid.通过这种方式,您可以在 DataGrid 中拥有默认列值。

Edit编辑

As you said the itemssource of column is a string there is one way I can think of..正如你所说,列的itemssource是一个字符串,我能想到一种方法..

Make it collection of SomeClass which will only have a string property.使它成为SomeClass集合,它只有一个string属性。 In the default constructor of SomeClass assign this string a default value.SomeClass的默认构造函数中,为此字符串分配一个默认值。 That default value will appear in your data grid column该默认值将出现在您的数据网格列中

Actually you can do this with the help of ValueConverters.实际上,您可以在 ValueConverters 的帮助下做到这一点。 When CanUserAddRows=True the DataContext of a row is binded to element of ItemsSource or to {DataGrid.NewItemPlaceholder} for the blank row.当 CanUserAddRows=True 时,行的 DataContext 绑定到 ItemsSource 的元素或绑定到空白行的 {DataGrid.NewItemPlaceholder}。

    <DataGrid ItemsSource="{Binding Path=Collection}" CanUserAddRows=True >
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Converter={StaticResource ShowSuitablePart}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

And that's a code of ShowSuitablePart converter.这是 ShowSuitablePart 转换器的代码。

public class ShowSuitablePart : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString() == "{DataGrid.NewItemPlaceholder}")
            return "This is blank row, just click me to create a new one";
        else
            ((YourCollectionObject)value).SomeProperty;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception();
    }
}

If you need TwoWay binding you'll have to use 2 elements in DataTemplate.如果需要双向绑定,则必须在 DataTemplate 中使用 2 个元素。 One for the blank row, the second for regular rows with twoway binding.一个用于空白行,第二个用于具有双向绑定的常规行。 Set Visibility binding to hide the first element for regular rows and second element for blank row.设置可见性绑定以隐藏常规行的第一个元素和空白行的第二个元素。

Remember if you determine Path to some property of you collection object it won't raise ValueConverter in blank row case.请记住,如果您确定集合对象的某些属性的路径,它不会在空行情况下引发 ValueConverter。

Create a CheckBox column on DataGrind and bind it to "myBoolColumn"在 DataGrind 上创建一个 CheckBox 列并将其绑定到“myBoolColumn”

<DataGridCheckBoxColumn Header="Selected" Binding="{Binding myBoolColumn}" />

DataTable data = new DataTable();
data.Columns.Add("myBoolColumn", typeof(System.Boolean));
data.Columns["myBoolColumn"].DefaultValue = false;

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

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