English 中文(简体)
当 DataGrid 没有数据时空行失踪
原标题:Blank Row missing when DataGrid has no data
  • 时间:2012-05-24 21:32:48
  •  标签:
  • wpf
  • datagrid

我正在研究WPF,我正在设计一个包含数据格的控制器, 并且一切都进展顺利。

但有些奇怪的事情刚刚发生:如果我在数据网格中显示一个或多个行, 数据网格在结尾处显示空行用来添加新行, 通常如此, 但是如果数据网格中没有行, 则也没有显示空行!

所以,我需要我的数据格 总是在末端的空行 不论数据是否。

我尝试了 CanUserAddRows 和 IsRead only 的属性,但正如我之前所说,只有当有一个或多个行时才有效。

有人知道如何做到这一点吗?

提前感谢您

最佳回答

您的意思是空白行吗? 请查看 < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

此基本设置确实显示一个空白行( 即使被约束的项目源没有数据)

XAML XAML

<Window x:Class="Demo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    >
<Grid>
    <DataGrid ItemsSource="{Binding Items}">
        <!--Must define columns if you need columns before bound collection has data-->
        <DataGrid.Columns>
            <DataGridTextColumn Header="Property"  Binding="{Binding Path=MyProperty}">   </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</Window>

后面的守则

public class Data
{
    public Data(string value) { MyProperty = value; }
    public Data() { }  // Must have default constructor
    public String MyProperty { get; set; }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{        
    List<Data> _items = new List<Data>(); 
    public List<Data> Items
    {
        get { return _items; }
    }

    public MainWindow()
    {
        InitializeComponent();
   }
问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...