MVC设计问题

发布于 2024-11-11 17:18:22 字数 646 浏览 3 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-11-18 17:18:22

绑定到源而不是 DataContext,源由 ElementName, RelativeSource & 来源,因此您可以命名视图,并使用 ElementName 将其作为源,然后使用 Path 可以是 DataContext.LookupValues 或模型中的任何属性(- View的DataContext是你的模型-)被调用的。


例如

<Window ...
    Name="Window">
    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" />
                    <ComboBox ItemsSource="{Binding ElementName=Window, Path=DataContext.Occupations}"
                            SelectedItem="{Binding Occupation}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <!-- ... --->

编辑:你的问题似乎是你没有传递你需要的信息,考虑一种设计,它仍然允许你访问不仅仅是某些列表的SelectedItem,例如

<Window ...
        Name="Window">
    <ListBox Name="listBox" ItemsSource="{Binding Data}" />
    <ContentControl DataContext="{Binding ElementName=listBox, Path=SelectedItem}">
        <ComboBox ItemsSource="{Binding ElementName=Window, Path=DataContext.Occupations}"
                SelectedItem="{Binding Occupation}" />
    </ContentControl>

    <!-- ... --->

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 use ElementName to get it as source then the Path could be DataContext.LookupValues or whatever your property in the model (- the DataContext of the View is your model -) is called.


e.g.

<Window ...
    Name="Window">
    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" />
                    <ComboBox ItemsSource="{Binding ElementName=Window, Path=DataContext.Occupations}"
                            SelectedItem="{Binding Occupation}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

    <!-- ... --->

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.

<Window ...
        Name="Window">
    <ListBox Name="listBox" ItemsSource="{Binding Data}" />
    <ContentControl DataContext="{Binding ElementName=listBox, Path=SelectedItem}">
        <ComboBox ItemsSource="{Binding ElementName=Window, Path=DataContext.Occupations}"
                SelectedItem="{Binding Occupation}" />
    </ContentControl>

    <!-- ... --->

The DataContext of the ContentControl 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文