使用wpf中的附加列以编程方式实现propertygrid外观

时间:2013-11-13 09:47:13

标签: c# wpf wpf-controls propertygrid

我很难实现什么是一个propertyGrid类型的控件,但在我的情况下,对于给定的属性,我之前有值,后面有值。例如如果属性是名称,它具有两个值,例如之前的名称和名称之后。

class Person
    {

        #region  class constructor
        /// <summary>
        /// class constructor
        /// </summary>
        /// <param name="_person"></param>
        public Person(string before,string after)
        {
            this.nameBefore = before; // assign beforename to namebefore
            this.nameAfter = after;  // assign aftername to nameAfter
        }

        #endregion class constructor

        #region properties

        /// <summary>
        /// Name property
        /// </summary>
        private string nameBefore;

        /// <summary>
        /// public Property
        /// for nameBefore
        /// </summary>
        public string NameBefore
        {
            get { return nameBefore; }
            set { nameBefore = value; }
        }


        /// <summary>
        /// name property after
        /// </summary>
        private string nameAfter;


        /// <summary>
        /// public property for nameAfter
        /// </summary>
        public string NameAfter
        {
            get { return nameAfter; }
            set { nameAfter = value; }
        }

        #endregion properties

    }

我需要公开这个类的所有属性,它们之前可能有值,后面有值。这些值是在外部设置的。我需要像propertygrid一样填充这些值。但在我的情况下,我还有一个额外的列(后面的值)。

这就是我的xaml的样子:

<Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition  Height="20" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Background="Gray" Padding="5,2,5,5" Grid.Column="0" Grid.Row="0" Text="Property" />
        <TextBlock Background="Gray" Padding="5,2,5,5" Grid.Column="1" Grid.Row="0"  Text="Before" />
        <TextBlock Background="Gray" Padding="5,2,5,5" Grid.Column="2" Grid.Row="0" Text="After" />

        <TextBlock Grid.Column="0" Grid.Row="1" Text="Name" />
        <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Before }" />
        <TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding After }" />

    </Grid>

我需要这样的东西

属性窗口

Property Window

我们能以编程方式获得这样的内容吗?我的意思是拥有一个属性及其多重价值。它也可以是多键值字典吗?

1 个答案:

答案 0 :(得分:1)

如果你复制每一处房产,你会浪费很多时间。而不是这样做,只需复制包含属性的类。通过这种方式,你可以这样做:

Grid的每一行定义一个类(用于显示目的):

public class PropertyGridRow : INotifyPropertyChanged
{
    public string PropertyName { get; set; }
    public object PropertyValueBefore { get; set; }
    public object PropertyValueAfter { get; set; }
}

然后你可以像这样填充它,或者在循环中略有不同:

Person before = GetBeforePerson();
Person after = GetAfterPerson();
...
PropertyGridRow propertyGridRow = new PropertyGridRow();
propertyGridRow.PropertyName = "Some Property";
propertyGridRow.PropertyValueBefore = before.SomeProperty;
propertyGridRow.PropertyValueAfter = after.SomeProperty;
PropertyGridRows.Add(propertyGridRow);

然后显示如下:

<GridView ItemsSource="{Binding PropertyGridRows}" ... />