gpt4 book ai didi

silverlight - Dataform Silverlight 中的数据绑定(bind)组合框在 Update 中使用 MVVM

转载 作者:行者123 更新时间:2023-12-03 10:24:36 27 4
gpt4 key购买 nike

我有主/详细信息 - 数据网格/数据表单,在选择项目后它显示在数据表单中以进行更新,但我在数据绑定(bind)或用部门填充组合框并将 SelectedEmployee.departmentid 设置为选定值时遇到问题。

现在这里有2个问题:

1. 在 EmployeeViewModel 中,这段代码不起作用,为什么?

  private ObservableCollection<department> _departments;
public ObservableCollection<department> Departments
{
get { return _departments; }
set
{
_departments = value;
RaisePropertyChanged("Departments");
}
}

但是这段代码工作正常
   private ObservableCollection<department> _departments;
public ObservableCollection<department> Departments
{
get {

if (_departments == null)
{
_departments = new ObservableCollection<department> {
new department()
{ id = 1, departmentname = "Technical " + 1, },
new department()
{ id = 2, departmentname = "Technical " + 2, },
new department()
{ id = 3, departmentname = "Technical " + 3, }
};
}
return _departments;
}
set
{
_departments = value;
RaisePropertyChanged("Departments");
}
}

2. DataForm 内外Combobox 的行为不同。外面可以,里面不行。我觉得这里需要用到ItemsSource中的Source,但是不知道怎么用。那么还有一个问题是如何解决呢?

员工 View .xaml
<navigation:Page    xmlns:local="clr-namespace:departmentTechManager"
x:Class="departmentTechManager.Views.employeeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="820" d:DesignHeight="780"
Title="employees"
Style="{StaticResource PageStyle}"

xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvmlightcmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:dataformtoolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"

DataContext="{Binding employeeStatic, Source={StaticResource Locator}}">

<data:DataGrid Grid.Row="0" x:Name="dgEmployees" CanUserSortColumns="true"
IsReadOnly="true" AutoGenerateColumns="true"
ItemsSource="{Binding Employees}"
SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" Margin="0,36,-123,0"></data:DataGrid>
<dataformtoolkit:DataForm x:Name="dfDetails"
CurrentItem="{Binding SelectedEmployee}" AutoGenerateFields="False"
CommitButtonContent="Save" CommandButtonsVisibility="Edit, Commit, Cancel">
<dataformtoolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataformtoolkit:DataField Label="name">
<TextBox Text="{Binding name, Mode=TwoWay}" /></dataformtoolkit:DataField>

<dataformtoolkit:DataField Label="departments">


<ComboBox ItemsSource="{Binding Departments}"
DisplayMemberPath="departmentname"
SelectedValuePath="id"
SelectedValue="{Binding Path=SelectedEmployee.departmentid, Mode=TwoWay}" />


</dataformtoolkit:DataField>
</StackPanel>
</DataTemplate>
</dataformtoolkit:DataForm.EditTemplate>
<i:Interaction.Triggers><i:EventTrigger EventName="EditEnded">
<mvvmlightcmd:EventToCommand Command="{Binding SaveEmployeesCommand}"/>
</i:EventTrigger></i:Interaction.Triggers>
</dataformtoolkit:DataForm>

在 ViewModelLocator.cs 中:
 public ViewModelLocator()
{

_sp = ServiceProviderBase.Instance;

Createdepartment();

Createemployee();

}

#region EmployeeViewModel
private static EmployeeViewModel _employee;

public static EmployeeViewModel employeeStatic
{
get
{
if (_employee == null)
{
Createemployee();
}

return _employee;
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public EmployeeViewModel employee
{
get
{
return employeeStatic;
}
}

public static void Clearemployee()
{
//do it later
//_employee.Cleanup();
_employee = null;
}

public static void Createemployee()
{
if (_employee == null)
{
_employee = new EmployeeViewModel(_sp.PageConductor, _sp.EmployeeDataService);
}
}
#endregion

#region DepartmentViewModel
private static DepartmentViewModel _department;

public static DepartmentViewModel departmentStatic
{
get
{
if (_department == null)
{
Createdepartment();
}

return _department;
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public DepartmentViewModel department
{
get
{
return departmentStatic;
}
}

public static void Cleardepartment()
{
//do it later
//_department.Cleanup();
_department = null;
}

public static void Createdepartment()
{
if (_department == null)
{
_department = new DepartmentViewModel(_sp.PageConductor, _sp.DepartmentDataService);
}
}

#endregion

有人可以帮助我吗?

组合框只是空的。但现在我可以用这样的部门填充它:

departmentTechManagerDomainService.metadata.cs
 [MetadataTypeAttribute(typeof(employee.employeeMetadata))]
public partial class employee
{
[Include]
public department department { get; set; }
public Nullable<int> departmentid { get; set; }
public string name { get; set; }
}

部门TechManagerDomainService.cs
public IQueryable<employee> GetEmployees()
{return this.ObjectContext.employees.Include("department").OrderBy(e=>e.name);}

这是 ViewModel 代码:
        private ObservableCollection<department> _departments;
public ObservableCollection<department> Departments
{
get { return _departments; }
set
{
_departments = value;
RaisePropertyChanged("Departments");
}
}

private department _selectedDepartment;
public department SelectedDepartment
{
get { return _selectedDepartment; }
set
{
_selectedDepartment = value;
RaisePropertyChanged("SelectedDepartment");
}
}

private void InitializeModels()
{
Employees = new ObservableCollection<employee>();
SelectedEmployee = new employee();
NewEmployee = new employee();

//new
Departments = new ObservableCollection<department>();
SelectedDepartment = new department();

}

private void GetEmployeesCallback(IEnumerable<employee> employees)
{
if (employees != null)
{
foreach (var employee in employees)
{
Employees.Add(employee);
//new
if (!Departments.Contains(employee.department))
Departments.Add(employee.department);

}
if (Employees.Count > 0)
{
SelectedEmployee = Employees[0];
}

}
}

我使部门不同,但这里只有那些已经被选中的部门,但这里不是那些尚未被选中的部门,而且组合框仍然没有填充 DataForm 中的部门。 ?!

最佳答案

第二个问题 - 它看起来像 combobox内外dataform接收不同DataContext因此试图定位 Departments不同来源的属性。目前尚不清楚如何修复它,因为您还没有展示大部分 ViewModel。

关注VS output窗口,它通常提供有关绑定(bind)错误的非常详细的信息,我假设您的情况存在绑定(bind)错误。

尝试通过以下方式修改与部门相关的绑定(bind):

<ComboBox ItemsSource="{Binding DataContext.Departments, RelativeSoruce={RelativeSource AncestorType={x:Type localViews:employeeView}}}" />

在哪里 localViews应该是 departmentTechManager.Views 的 xml 命名空间.对 SelectedItem 尝试相同的技巧捆绑。

关于silverlight - Dataform Silverlight 中的数据绑定(bind)组合框在 Update 中使用 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5042596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com