0

我有点坚持实现一种 propertyGrid 类型的控件,但在我的情况下,对于给定的属性,我在之前和之后都有价值。例如,如果属性是名称,则它具有两个值,例如 name before 和 name after。

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

    }

我需要公开这个类的所有属性,这些属性可能在之前和之后都有值。这些值是在外部设置的。我需要像属性网格一样填充这些值。但就我而言,我还有一个额外的列(之后的值)。

这就是我的 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>

我需要这样的东西

属性窗口

属性窗口

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

4

1 回答 1

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}" ... />
于 2013-11-13T13:57:24.560 回答