MVC设计问题
我有一个使用 MVC 的应用程序。它有一个画布和属性网格。当在画布中选择一个项目时。属性网格应显示其详细信息。
因此,我创建了一个事件侦听器,当在画布中选择项目时,它会向控制器引发一个事件,该控制器将所选项目传递到属性网格以显示详细信息。
模型:
包含名称、描述的项目对象
控制器:
protected Controller(object model, FrameworkElement view)
{
this._model = model;
this._view = view;
}
public virtual void Initialize()
{
View.DataContext = Model;
}
视图:
<TextBlock>Status</TextBlock>
<ComboBox ItemsSource="?????"/>
其中视图是属性网格,模型是所选项目。
问题是在属性网格中有一个包含查找值的下拉列表,鉴于属性网格的数据上下文已设置为不包含对这些查找项的引用的选定项目,我如何获取下拉值。
我知道使用自定义代码很容易做到这一点。但我不想违反 MVC 方法。
I'm having an application using MVC. It has a canvas and property grid. When an item is selected in the canvas. The property grid should display its details.
So I made an event listener and when item is selected in the canvas it raises an event to the controller which pass the selected item to the property grid to display the details.
Model :
Item object containing name, description
Controller :
protected Controller(object model, FrameworkElement view)
{
this._model = model;
this._view = view;
}
public virtual void Initialize()
{
View.DataContext = Model;
}
View :
<TextBlock>Status</TextBlock>
<ComboBox ItemsSource="?????"/>
Where view is the property grid and model is the selected item.
The problem is in the property grid there is a dropdown list containing lookup values how can I get the dropdown values given that the datacontext of the property grid has already been set to the selected item which doesn't contain reference to these lookup items.
I know that it's easy to use custom code to do that. But I don't want to violate the MVC aproach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定到源而不是 DataContext,源由
ElementName
,RelativeSource
&来源
,因此您可以命名视图,并使用ElementName
将其作为源,然后使用Path
可以是DataContext.LookupValues
或模型中的任何属性(- View的DataContext是你的模型-)被调用的。例如
编辑:你的问题似乎是你没有传递你需要的信息,考虑一种设计,它仍然允许你访问不仅仅是某些列表的SelectedItem,例如
DataContext<
ContentControl
的 /code> 可能是 ListBox 的 SelectedItem,但内部的 ComboBox 仍然可以引用 Window 的 DataContext,它应该提供必要的信息。这与我的第一个示例类似,DataTemplate 内的 DataContext 始终是集合的一项,但您可以使用绑定中的源来访问外部 DataContext。
Bind to a source rather than DataContext, sources are provided by
ElementName
,RelativeSource
&Source
, so you can name the View for example and useElementName
to get it as source then thePath
could beDataContext.LookupValues
or whatever your property in the model (- the DataContext of the View is your model -) is called.e.g.
Edit: Your problem seems to be that you do not pass the information you need, consider a design which still grants you access to more than just the SelectedItem of some list, e.g.
The
DataContext
of theContentControl
may be the SelectedItem of the ListBox but the ComboBox inside can still reference the DataContext of the Window which should provide the necessary information.This is similar to my first example in that the DataContext inside the DataTemplate is always an item of the collection but you can access external DataContexts using sources in your bindings.