要么我没有看到解决方案,要么我在使用MVVM时遇到了陷阱.
我有这个样本Master-Detail:
class Customer
{
int CustomerID {get;set}
string Name {get;set}
ObservableCollection<Order> Orders {get;set}
}
class Order
{
int OrderID {get;set}
int Quantity {get;set}
double Discount {get;set}
}
Run Code Online (Sandbox Code Playgroud)
让我们在CustomerOrdersViewModel中假设我的ObservableCollection客户绑定到View via ... ="{Binding Customers}",当客户从用户更改时,相关订单通过ItemsSource ="{Binding SelectedItem.Orders"显示在DataGrid中,ElementName = comboboxCustomer}".
这可以通过MVVM实现:
我可以简单地(为了简单起见)调用添加新客户Customers.Add(new Customer(){...});.
添加后我这样做:this.RaisePropertyChanged("Customers");.这将更新视图并立即在Customer-Combobox中显示客户.
现在是MVVM不可能实现的部分.
我可以添加一个新的Order by SelectedCustomer.Orders.Add(New Order(){...});
但是我现在无法像订单上的客户那样提出CollectionChanged/PropertyChanged事件,因为订单属性未通过公共访问者绑定到View.
即使我将Orders bindable属性暴露给视图,视图本身也会关注Master-Detail切换而不是ViewModel ......
题
如何使用详细信息列表中的添加/删除对象以及在视图上立即更新来使Master-Detail工作?
我发现实际上有两种方法可以使用mvvm模式处理控件上的鼠标事件.
两种方式实际上都是1种方式:
MVVM Light Toolkit http://mvvmlight.codeplex.com/
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems,
ElementName=MyDataGrid}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
和Blend interactivity.dll与行为
<i:Interaction.Triggers>
<i:EventTrigger EventName=”MouseLeftButtonDown”>
<Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
你知道更好的方法吗?
主持人:为什么我的最后6个xaml代码行根本不可见?它们被IE和Iron浏览器吞噬.您能否报告管理员修复该代码脚本?它经常不起作用.证明:http://img251.imageshack.us/img251/5236/errorxt.png
是否只能通过使用诸如 之类的属性注释我的数据类来将类别添加到 .NET PropertyGrid CategoryAttribute?
我有一个.NET 4.0 WPF项目.
当我打开FileDialog时,选择一些文件然后按OK按钮,然后我在输出窗口中看到这个错误:
WindowsBase.dll中出现"System.ComponentModel.Win32Exception"类型的第一次机会异常
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
DialogResult result = fileDialog.ShowDialog();
if (result == DialogResult.OK)
{
Run Code Online (Sandbox Code Playgroud)
为什么我在if语句之前得到了Exception?
当DataGrid设置为IsReadOnly = FALSE时,为什么只有通过双击空单元格才能看到该列中的ComboBox?
<DataGridComboBoxColumn Width="*" IsReadOnly="False" Header="test" />
Run Code Online (Sandbox Code Playgroud)
使用DataTemplateColumn一如既往地工作...... DataGridComboBoxColumn有什么问题?
作品:
<DataGridTemplateColumn Header="Schoolclass">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Background="Blue" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud) 当我迭代它们时,我无法删除节点.
我有一个带Guid的列表.
我想删除该Xml文件中XElement具有该列表的Guid的所有XElements
多数民众赞成我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Departments>
<Department Id="2d55ba71-a2ab-44a1-a697-f57bbd238c7f" />
<Department Id="775cd4c2-74c9-4f41-9ddf-1126c508cccb" />
</Departments>
Run Code Online (Sandbox Code Playgroud)
这不起作用: xDoc.Descendants("Department ").Remove<XElement>().Where...
我怎样才能做到这一点?
list.Min()得到最小值为整数.我想获取List中具有特定属性X的最小值的对象.我该怎么做?
我有一点困惑
来自wiki:"这意味着真正的模拟......对传递给方法的数据执行测试会调用为参数."
我从未使用过单元测试或模拟框架.我认为单元测试是针对自动化测试的,那么什么是模拟测试呢?
我想要的是一个替换我的数据库的对象,我可能会在以后使用,但仍然不知道我使用的是什么数据库或orm工具.
当我使用模拟程序执行我的程序时,我可以轻松地用POCO替换它们以使实体框架例如工作得非常快吗?
编辑:我不想使用单元测试,但使用Mocks作为实体+数据库的完全替代将是不错的.
我只是在编写一个实现ServiceLocator模式的类.
public class ServiceFactory : IServiceFactory
{
private IDictionary<Type, object> instantiatedServices;
public ServiceFactory()
{
instantiatedServices = new Dictionary<Type, object>();
}
public T GetService<T>() where T : class, new()
{
if (this.instantiatedServices.ContainsKey(typeof(T)))
{
return (T)this.instantiatedServices[typeof(T)];
}
else
{
T service = new T();
instantiatedServices.Add(typeof(T), service);
return service;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有几个问题:
1.)我应该从哪里打电话给这个班级?app.xaml.cs做wpf的东西?
2.)我应该注册服务,如果是,我应该在哪里注册?
3.)当我对服务"ICustomerService"进行延迟初始化时,为什么我应该为它创建一个Register(T服务)方法呢?这是双重工作.
4.)我应该去找服务定位器吗?
UPDATE
目前我觉得我必须为我个人目的强奸DI工具=>
App.xaml.cs =>这里我创建MainWindow并将其datacontext设置为MainViewModel.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var mainVM = new MainViewModel();
var mainWindow = new …Run Code Online (Sandbox Code Playgroud) 使用像Unity,AutoFac或其他类似的IOC容器,您必须注册并解析IInterface以获取实例.这是你在app类中做的所有根.
在完成Register/Resolve之后,我正在创建我的MainController并将它们传递给所有已解析的服务,例如:
protected void Application_Start(object sender, EventArgs e)
{
var builder = new ContainerBuilder();
builder.Register<IUserService1, UserService1>();
builder.Register<IUserService2, UserService2>();
builder.Register<IUserService3, UserService3>();
builder.Register<IAnotherService, AnotherService>();
// And many more Services...
_container = builder.Build();
var userService1 = _container.Resolve<IUserService1>();
var userService2 = _container.Resolve<IUserService2>();
var userService3 = _container.Resolve<IUserService3>();
var anotherService = _container.Resolve<IAnotherService>();
var vm = new MainController(userService1,userService2,userService3,anotherService)
}
public class MainController
{
private UserController1 _userVM1;
private UserController2 _userVM2;
private UserController3 _userVM3;
public MainController(IUserService1 userService1,IUserService2 userService2,IUserService3 userService3,anotherService)
{
_userVM1 = new UserController1(userService1,anotherService);
_userVM2 = new UserController2(userService2,...,...);
_userVM3 …Run Code Online (Sandbox Code Playgroud)