简体   繁体   English

当ItemsSource为空时,如何确保WPF DataGrid至少显示一行? (可编辑的ComboBox单元格)

[英]How can I make sure my WPF DataGrid shows at least one row when ItemsSource is empty? (Editable ComboBox cells)

I have a DataGrid that allows a user to edit properties of electronic devices such as what inputs and outputs the devices have for the purposes of drawing schematics. 我有一个DataGrid,它允许用户编辑电子设备的属性,例如,为了绘制原理图,设备具有哪些输入和输出。 There may be scenarios in which the user is editing a previously saved device - in this case the ItemsSource will be bound an already defined object's properties. 在某些情况下,用户正在编辑以前保存的设备-在这种情况下,ItemsSource将绑定到已经定义的对象的属性。 More often, the user will be creating a new device from scratch, so the ItemsSource will be empty to start. 用户通常会从头开始创建新设备,因此ItemsSource将为空以启动。 When this is the case, my DataGrid is completely blank, and I want it to show one blank row. 在这种情况下,我的DataGrid完全空白,我希望它显示一个空白行。 My cells are made up of editable ComboBoxes. 我的单元格由可编辑的组合框组成。

I'm so far unable to get the DataGrid to show a previously defined ItemsSource, as well as show a blank row when ItemsSource is empty. 到目前为止,我无法使DataGrid显示先前定义的ItemsSource,以及在ItemsSource为空时显示空白行。

XAML: XAML:

<DataGrid Grid.Column="0" Grid.ColumnSpan="2"
              Grid.Row="1" Grid.RowSpan="2"
              ScrollViewer.VerticalScrollBarVisibility="Auto"
              ItemsSource="{Binding InputList}">
        <DataGrid.Columns>

            <DataGridTemplateColumn Header="Label" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox IsEditable="True"
                                    Text="{Binding NewLabel}"
                                    ItemsSource="{Binding Label}"
                                    SelectedValue="{Binding SelectedLabel}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Signal" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox IsEditable="True"
                                    Text="{Binding NewSignal}"
                                    ItemsSource="{Binding Signal}"
                                    SelectedValue="{Binding SelectedSignal}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Terminal" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox IsEditable="True"
                                    Text="{Binding NewTerminal}"
                                    ItemsSource="{Binding Terminal}"
                                    SelectedValue="{Binding SelectedTerminal}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>

    </DataGrid>

Bindings are as follows: InputList - a list of Structs {Label, Signal, Terminal} for a previously defined device. 绑定如下:InputList-先前定义的设备的结构{Label,Signal,Terminal}的列表。 New structs will be created upon saving if this is a new device. 如果这是新设备,则在保存时将创建新结构。 This List(IOStruct) is a property of my device object. 此列表(IOStruct)是我的设备对象的属性。

NewLabel - user can type a new Label if it is one that hasn't been used previously Labels - master list of previously defined labels (for quick adding using autocomplete) NewLabel-如果以前未使用过,则用户可以键入新标签。Labels-先前定义的标签的主列表(用于使用自动完成功能进行快速添加)

SelectedLabel - this will be used to generate the new struct if the user chooses a previously defined label SelectedLabel-如果用户选择先前定义的标签,它将用于生成新的结构

Next two columns have similar bindings but for Signal types and Terminal types. 接下来的两列具有类似的绑定,但针对信号类型和终端类型。

Running the application without InputList defined just gives me a blank DataGrid that I cannot do anything with. 在未定义InputList的情况下运行应用程序只会给我一个空白的DataGrid,我无法执行任何操作。 I would like there to be a blank row in this case. 在这种情况下,我希望有一个空白行。

Any help or tips to make this work and improve my practices is greatly appreciated! 任何帮助或技巧,使这项工作,并改善我的做法,万分感谢!

You can try something like this... 您可以尝试这样的事情...

First add a extra column in DataGrid.. 首先在DataGrid中添加一个额外的列。

 <DataGridTemplateColumn Header="Display Order" Width="96" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding DisplayOrder}" Margin="0" VerticalAlignment="Center" TextAlignment="Right"
                                 HorizontalAlignment="Stretch" x:Name="txtDisplayOrder"  Tag="{Binding}" 
                                 Loaded="txtDisplayOrder_Loaded" MaxLength="1" Background="Transparent" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

The DisplayOrder field should be present in your ItemsSource of type int.. DisplayOrder字段应该出现在int类型的ItemsSource中。

The Loaded event should be like this Loaded事件应如下所示

    private void txtDisplayOrder_Loaded(object sender, RoutedEventArgs e)
    {
        TextBox txt = sender as TextBox;
        ValidationRuleInteger objDisplayOrder = new ValidationRuleInteger("Display Order", true, false);
        if (txt != null && objDisplayOrder != null)
            txt.GetBindingExpression(TextBox.TextProperty).ParentBinding.ValidationRules.Add(objDisplayOrder);

    }

And finally the class ValidationRuleInteger should be as follows: 最后,类ValidationRuleInteger应该如下所示:

 public class clsValidationRuleInteger :ValidationRule
{
    public string strmsg;
    public bool isRequired;
    public bool? isRegex;
    bool AllowNegativeValues;

    public clsValidationRuleInteger(string strmsg, bool isRequired, bool _AllowNegativeValues)
    {
        this.strmsg = strmsg;
        this.isRequired = isRequired;
        AllowNegativeValues = _AllowNegativeValues;
    }

   public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        throw new NotImplementedException();
    }
}

This should work fine as per your requirement... 根据您的要求,这应该可以正常工作...

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

相关问题 如何在WPF中制作可编辑的DataGrid? - How can I make an editable DataGrid in WPF? 在wpf datagrid中itemssource为null时显示空行 - Show an empty row when itemssource is null in wpf datagrid C#WPF基于它的ItemsSource使DataGrid TemplateColumn ComboBox可编辑 - C# WPF Make DataGrid TemplateColumn ComboBox editable based on it's ItemsSource 当我在WPF中选择一行数据网格时,如何在组合框中显示一个值? - How to show a value in combobox when i select a row of datagrid in wpf? 我如何确定提交时至少点击了一个电台 - How can I make sure that they are at least one radio been click when submit 如何使DataGrid行中的ComboBox唯一? - How can I make a ComboBox in a DataGrid row unique? WPF DataGrid - 如何在按钮单击时使选定的行可编辑 - WPF DataGrid - How to make selected row editable on Button click 如何在DataGrid WPF桌面应用程序中进行行分组 - How I can make Row Grouping in DataGrid WPF Desktop Application C#WPF工具包:如何​​使数据网格中的单元格可编辑? - C# WPF toolkit: How can I make a cell from a datagrid be editable? WPF DataGrid - 从 DataGrid ItemsSource 对象的集合值中设置每行(对象)唯一的 combobox 值 - WPF DataGrid - Set unique per row (object) combobox values from the DataGrid ItemsSource object's collection value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM