Adding rows to DataGrid through XAML

前端 未结 1 1152
醉酒成梦
醉酒成梦 2021-02-13 03:19

Is it possible to add one or more rows to WPF DataGrid through XAML, without binding it to a collection. What I\'m looking for would essentially be something like:



        
1条回答
  •  无人共我
    2021-02-13 04:04

    Feeling lucky. Found it myself. Here's the simplest way.

    Create a dummy class with the same public properties (important that you define members as properties and not fields). For example:

    public class Dummy
    {
        public string Subject { get; set; }
        public string Body { get; set; }
        public DateTime DueDateStart { get; set; }       
    }
    

    Import your project namespace into XAML by adding the following import at the top:

    xmlns:local="clr-namespace:YourProjectNamespace"
    

    Now you can add items (rows) to your DataGrid at design-time like (make sure your columns have proper bindings):

    
        
          
          
          
        
    
        
        
    
    

    Hope this helps someone!

    Update

    Since this is now a popular post, I thought I should update this with the standard way of doing things.

    WPF supports a concept known as Design-Time Data that serves this exact purpose. A few key advantages of using Design-Time Data over the approach I mentioned above include:

    1. Design-Time Data remains separate from application functionality.
    2. You do not need to change anything at all to switch your controls between design and run modes.
    3. Data lives in XML files that are easily editable.

    Here are the steps to create design-time data files:

    1. Open your project in Blend (comes free with VS2015 and VS2017).
    2. Open your View (Window or Control that you're working with).
    3. From Data tool window (docked with Solution Explorer by default), choose Create Sample Data From Class.
    4. Select your VM class. You should choose the same class that your control will actually use at runtime as its DataContext.
    5. Blend will create an XML file with sample data automatically filled in for you. The file will look something like this:

      
        
        
        
      
      

    Important to note that you do not need Blend to generate this file. You can do this by hand too.

    1. Now in your DataGrid (or whatever control you're working with), add the following property (change file path according to your project):

      d:DataContext="{d:DesignData Source=SampleData/TestDataListSampleData.xaml}"
      
    2. Given that your control has its properties properly set (e.g. ItemsSource, Columns etc.), sample data will start showing in the designer immediately.

    Just for completion, note that Blend cannot generate automatic data for generic classes. For example, if your VM class contains a property of type List (or if the VM class itself is a generic class), you'll not see that property getting generated in the sample data file. In that case, you must create your own dummy class inheriting from the generic class and then use it as the type of your property. For example:

    public class MyListOfStrings : List
    { }
    

    0 讨论(0)
提交回复
热议问题