MVVM:自定义devexpress propertyGridControl

时间:2014-09-15 09:27:41

标签: c# wpf xaml mvvm devexpress

我的XAML中有一个PropertyGridControl。它显示了GridControl的选定行中对象的属性。当我单击一行时,它会填充PropertyGridControl。我正在使用MVVM模式。

propertyGridControl的XAML是:

<dxprg:PropertyGridControl  SelectedObject="{Binding SelectedItem,ElementName=lst1}" ShowProperties="All" ShowCategories="False" Margin="473,0,0,133"/>

但是如何自定义PropertyGridControl? 实施例:

来自:

enter image description here

到此: enter image description here

对我来说,焦点是自定义单选按钮元素中数据库的boolean / tinyint字段和组合框中的外部键。

FOR DMITRIG:

按照提示,现在我有了我的xaml文件,其中包含:

<dxlc:DataLayoutControl Grid.Column="1" CurrentItem="{Binding GroupedLayoutData}" HorizontalAlignment="Left" Height="204" Margin="10,132,0,0" VerticalAlignment="Top" Width="278"/>

和我的viewModel.cs文件,我在后面的示例中创建了一个UI。但我什么都看不到,错误在哪里?

   public class GroupedLayoutData {
        const string JobGroup = "Job";
        const string ContactGroup = "Contact";
        const string AddressGroup = "Address";
        const string PersonalGroup = "Personal";

        [Display(GroupName = AddressGroup, ShortName = "", Order = 4)]
        public string AddressLine1 { get; set; }
        [Display(GroupName = AddressGroup, ShortName = "")]
        public string AddressLine2 { get; set; }
        [Display(GroupName = PersonalGroup, Name = "Birth date")]
        public DateTime BirthDate { get; set; }
        [Display(GroupName = ContactGroup)]
        public string Email { get; set; }
        [Display(Name = "First name", Order = 0)]
        public string FirstName { get; set; }
        [Display(GroupName = PersonalGroup, Order = 5)]
        //public Gender Gender { get; set; }
        //[Display(GroupName = JobGroup, Order = 2)]
        public string Group { get; set; }
        [Display(GroupName = JobGroup, Name = "Hire date")]
        public DateTime HireDate { get; set; }
        [Display(Name = "Last name", Order = 1)]
        public string LastName { get; set; }
        [Display(GroupName = ContactGroup, Order = 3), DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
        [Display(GroupName = JobGroup), DataType(DataType.Currency)]
        public decimal Salary { get; set; }
        [Display(GroupName = JobGroup, Order = 21)]
        public string Title { get; set; }
    }

编辑ii:如何将定义布局的类绑定到datalayoutcontrol?

2 个答案:

答案 0 :(得分:4)

使用DevExpress完成您描述的任务时,我建议您尝试使用DataLayoutControl。当您通过DataLayoutControl.CurrentItem属性将DataLayoutControl绑定到任何数据对象时,此控件将自动构建一个布局来编辑对象的公共属性。

使用DataLayoutControl和Grid的最小示例:

模型(C#代码):

public class Person {
    const string PersonalGroup = "Personal";
    [Display(Name = "First Name", GroupName = PersonalGroup)]
    public string FirstName { get; set; }
    [Display(Name = "Last Name", GroupName = PersonalGroup)]
    public string LastName { get; set; }
    [Display(Name = "Birth date", GroupName = PersonalGroup)]
    public DateTime BirthDate { get; set; }
    [Display(GroupName = PersonalGroup)]
    public int Age { get; set; }

    [Display(GroupName = "Contact"), DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }
}

ViewModel(C#代码)

public class ViewModel {
    public ViewModel() {
        Persons = new ObservableCollection<Person> { 
            new Person(){ FirstName = "Peter", LastName ="Parker", Age=33 },
            new Person(){ FirstName = "Mary", LastName ="Jane" , Age=31 }
        };
    }
    public ObservableCollection<Person> Persons { get; private set; }
    public Person SelectedPerson { get; set; }
}

查看(XAML-markup):

<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <dxg:GridControl Grid.Column="0" 
            AutoGenerateColumns="AddNew"
            ItemsSource="{Binding Persons}" 
            SelectedItem="{Binding SelectedPerson}"
        >
        <dxg:GridControl.View>
            <dxg:TableView ShowGroupPanel="False" BestFitArea="All" AllowEditing="False"/>
        </dxg:GridControl.View>
    </dxg:GridControl>
    <dxlc:DataLayoutControl Grid.Column="1"
            CurrentItem="{Binding SelectedPerson}"
       />
</Grid>

结果(截图): enter image description here

您可以在this demo中查看此方法(链接指向Silverlight版本,但WPF版本的行为完全相同)。您可以使用System.ComponentModel.DataAnnotations命名空间中的属性,也可以使用Fluent-API来管理属性编辑器生成过程。

<强>更新
您可以使用DataLayoutControl.AutoGeneratingItem事件更改任何属性的编辑器。这种方法已在以下DevExpress支持线程中讨论过:
    - How to Customize controls inside WPF DataLayoutControl
    - WPF DataLayoutControl questions
    - DataLayoutControl

P.S。将来,我建议您使用DevExpress Support Center获取有关使用DevExpress产品的官方和保证协助。

答案 1 :(得分:0)

尝试使用FocusedRowChanged事件的网格视图。

propertyGridControl1.SelectedObject = gridView1.GetFocusedRow();