开发者

MVC design issue

开发者 https://www.devze.com 2023-03-10 02:31 出处:网络
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.

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. Bu开发者_开发技巧t I don't want to violate the MVC aproach.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号