creating a nice GUI in WPF

前端 未结 2 1924
旧时难觅i
旧时难觅i 2021-02-01 11:12

I need to create a desktop CAD application which essentially should have a nice modern GUI. I am thinking of creating a WPF application so that I can have a rich user interface.

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 11:38

    I think Microsoft saw no point in including a PropertyGrid control in WPF because it is so trivial to create your own, and if they created the control it would be harder to style.

    To create your own PropertyGrid, just use a with an that has a containing a docked to the left for the property name and a for the value editor, then enable grouping on the Category property.

    The only code you need to write is the code that reflects on the object and creates the list of properties.

    Here is a rough idea of what you would use:

    DataContext =
      from pi in object.GetType().GetProperties()
      select new PropertyGridRow
      {
        Name = pi.Name,
    
        Category = (
          from attrib in pi.GetCustomAttributes(false).OfType()
          select attrib.Category
        ).FirstOrDefault() ?? "None",
    
        Description = (
          from attrib in pi.GetCustomAttributes(false).OfType()
          select attrib.Description
        ).FirstOrDefault(),
    
        Editor = CreateEditor(pi),
    
        Object = object,
      };
    

    The CreateEditor method would simply construct an appropriate editor for the property with a binding to the actual property value.

    In the XAML, the would be something like this:

    
      
        
        
      
    
    

    I'll let you fill in the rest of the details.

提交回复
热议问题