添加新行时,WPF和EF将组合框设置为默认值 [英] WPF and EF setting combobox to default value when adding new row

查看:96
本文介绍了添加新行时,WPF和EF将组合框设置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF和EF的新手,并且对添加新行时如何将组合框设置为默认值(第一个项目)有疑问.我还没有找到一种通过实现IDataErrorInfo的部分扩展EF类进行验证的方法.我研究了互联网,但没有发现对我有用的东西.为了得到一些东西,我只是对逻辑进行了硬编码,以默认后面的代码中的组合框. (我还不是要学习MVVM的人.)

1. XAML中有办法吗?

-在Code Behind中添加新内容时设置组合框的方法

I am new to WPF and EF and have a question on how to set a combobox to a default value (1st item) when adding a new row. I have not found a way to validate it through a partial extended EF class implementing IDataErrorInfo. I researched the Internet but found nothing that would work for me. To get something, I just hardcoded logic to default the combobox in the code behind. (I am not at the point of learning MVVM yet).

1. Is there a way in XAML?

-- method to set combobox when adding new in Code Behind

private void AddClick(object sender, System.Windows.RoutedEventArgs e)
       {
           this.View.AddNew();

           // force lookup combobox to default
           this.Employee.SelectedIndex = 0;
           this.Subcontractor.SelectedIndex = 0;

           this.View.CommitNew();
       }



-XAML



-- XAML

<combobox name="Employee" iseditable="False">
                          ItemsSource="{Binding Source={StaticResource EmployeeLookup}}"
                          SelectedItem="{Binding Path=Employee}">
                    <itemscontrol.itemtemplate>
                        <datatemplate>
                            <stackpanel orientation="Horizontal">
                                <textblock text="{Binding Path=LastName }" />
                                <textblock text=", " />
                                <textblock text="{Binding Path=FirstName }" />
                            </stackpanel>
                        </datatemplate>
                    </itemscontrol.itemtemplate>
                </combobox>




2.我有另一个WPF页面,其中有一个以comboxbox作为列的列表视图.我尝试执行相同的逻辑,但是无法访问要设置的组合框控件.这里有什么想法吗?

TIA




2. I have another WPF page where I have a listview with a comboxbox as the column. I tried to do the same logic but cannot access the combobox control to set. Any ideas here?

TIA

推荐答案

行项目是否具有ViewModel对象?
如果是,那么每当您通过在集合中添加上述视图模型的对象在UI中显示新行条目时,都将Employee属性初始化为索引0值. (我说的是将Employee作为财产的类)
这将选择您在xaml绑定中定义的SelectedItem ="{Binding Path = Employee}"的项目.

有关示例,请参考: http://stackoverflow.com/questions/663881 /wpf-mvvm-combobox-selecteditem-or-selectedvalue-不工作 [ ^ ]
Do you have ViewModel object for row item?
If yes, then whenever you display new row entry in UI by adding object of above view model in collection, initialize Employee property to index 0 value. (I am talking about class having Employee as property)
This will select that item as you have defined SelectedItem="{Binding Path=Employee}" in your xaml binding.

For sample refer this : http://stackoverflow.com/questions/663881/wpf-mvvm-combobox-selecteditem-or-selectedvalue-not-working[^]


我不使用MVVM,因为我是WPF的新手.我正在使用Entity Framework 4.1,并且在此实体上具有扩展的局部类.


局部类Task:IDataErrorInfo
{

公共Task()
{
this.Number ="[新]";
this.StartDate = DateTime.Today;
this.EndDate = DateTime.Today;
this.ContractValue = 0;
this.FundingValue = 0;
this.Name = string.Empty;
this.SubObligation = 0;
this.Revenue = 0;
this.TravelObligation = 0;

}

....
单击添加"按钮以添加新实体时,将如上所述填充字段.组合框绑定到另一个Employee实体; Task和Employee实体之间的外键是一个ID字段.我不知道是否可以设置Task类的EmployeeID字段,以便组合框默认为New实体上的第一项.
I am not using MVVM since I am new to WPF. I am using Entity Framework 4.1 and have an extended partial class on this entity.


partial class Task:IDataErrorInfo
{

public Task()
{
this.Number = "[new]";
this.StartDate = DateTime.Today;
this.EndDate = DateTime.Today;
this.ContractValue = 0;
this.FundingValue = 0;
this.Name = string.Empty;
this.SubObligation = 0;
this.Revenue = 0;
this.TravelObligation = 0;

}

....
When clicking the Add button to add an new entity, the fields are populated as above. The combobox is bound to another Employee entity; the foreign key between the Task and Employee entities is an ID field. I don''t know if I can set the Task class EmployeeID field so that the combobox would default to the first item on a New entity.


这篇关于添加新行时,WPF和EF将组合框设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆