【发布时间】:2015-04-09 21:01:08
【问题描述】:
我正在进行我的第二个 WPF 项目并遇到了一个不舒服的情况。这是我的模型:
public class Layout
{
public string Name { get; set; }
public Motor Motor { get; set; }
}
public class Motor
{
public string Property1 { get; set; }
...
public string Property150 { get; set; }
}
这是我的 MainViewModel:
public class MainViewModel : ViewModelBase
{
public Motor Motor {get; set;}
...
}
好的,我已将电机类的所有 150 个属性绑定到文本框:
<TextBox Text="{Binding Layout.Motor.Property1}"/>
问题 1)如果我想在用户更改其中一项时触发一个操作,我是否必须执行 150 次??
private string property1 { get; set; }
public string Property1
{
get { return property1; }
set
{
property1 = value;
RaisePropertyChanged(() => Property1);
}
}
问题 2) 我必须在 MainViewModel 中,在模型“Motor”中实现它还是需要一个“MotorViewModel”?任何这些都意味着大量的复制和粘贴以及完全无用的编码..
感谢您的任何帮助和反馈!
【问题讨论】:
-
为什么要绑定 150 个属性到文本框?哪个用户会在视图中阅读/填充/编辑所有这些内容?
-
哇,这读起来很完美......它是...... [魔法]!很遗憾,默认情况下不包括在内。感谢您的链接
-
为什么你的模型有 150 个属性,而不是一个
List<string>之类的?