繁体   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)

我有一个DataGrid,它允许用户编辑电子设备的属性,例如,为了绘制原理图,设备具有哪些输入和输出。 在某些情况下,用户正在编辑以前保存的设备-在这种情况下,ItemsSource将绑定到已经定义的对象的属性。 用户通常会从头开始创建新设备,因此ItemsSource将为空以启动。 在这种情况下,我的DataGrid完全空白,我希望它显示一个空白行。 我的单元格由可编辑的组合框组成。

到目前为止,我无法使DataGrid显示先前定义的ItemsSource,以及在ItemsSource为空时显示空白行。

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>

绑定如下:InputList-先前定义的设备的结构{Label,Signal,Terminal}的列表。 如果这是新设备,则在保存时将创建新结构。 此列表(IOStruct)是我的设备对象的属性。

NewLabel-如果以前未使用过,则用户可以键入新标签。Labels-先前定义的标签的主列表(用于使用自动完成功能进行快速添加)

SelectedLabel-如果用户选择先前定义的标签,它将用于生成新的结构

接下来的两列具有类似的绑定,但针对信号类型和终端类型。

在未定义InputList的情况下运行应用程序只会给我一个空白的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>

DisplayOrder字段应该出现在int类型的ItemsSource中。

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);

    }

最后,类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();
    }
}

根据您的要求,这应该可以正常工作...

暂无
暂无

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

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