1

I've been reading some into it, but I am time pressed, so I would require a simple solution now and I promise to read up on it later. I come from a winforms c# background, and have lately been working with WPF. As the user interface I'm implementing is not that input intensive, I've gotten by with using INotifyPropertyChanged so far. Until I ran into this:

I have a 3D interface from a third party library (MogreInWpf). Upon clicking an object in the 3D interface, I need to display some of the object's properties (position, size etc). Here's how my code-behind looks. I have a class, let's call it objectOfInterest, that saves all these properties, the 3D data and includes some methods that manipulate the object. In one of these methods, I generate a Grid containing textboxes to simulate name-value pairs of properties. Like "X position:" "134". As these properties are only user-changed, I've managed to bind the textbox on text changed event to respond to most changes, IE when the size is changed from 134 to 1345.

Once this grid is generated, it is set to display in the main window using a ContentPresenter and setting its content to the objectOfInterest.grid. Works like a charm. Now, I have to implement a property that changes how the properties grid look like depending on a combobox included in this grid.

For example, if the object is a cube, I want width/height/length to show, while I only want a radius property to show if it's a sphere. I'm kind of stuck here, as regenerating the grid upon combobox selectedValueChanged doesn't notify the view, and the generating occurs in the objectOfInterest class instead of the main window class, meaning I can't rebind the grid once it's regenerated (I cannot call contentPresenter.Content = objectOfInterest.grid again, as the main window has no idea the instances grid has changed). So, without completely understanding the MVVM methods and rewriting most of the application to fit the model, how can this be accomplished?

1
  • I temporarily solved this by passing the ContentPresenter instance to the objectOfInterest instance and redefining its content there when the grid regeneration is set of. I'd still like to know of a better way though.
    – JasonX
    Commented Dec 14, 2013 at 14:52

1 Answer 1

1

If I understand correctly, you essentially have a ContentPresenter and you want the contents to change depending on the type of object being presented.

It sounds like you have created an ObjectOfInterest class that contains the object of interest, say a Cube or a Sphere, and it generates a Grid based on the type of object it contains. If I understand you correctly, then...

I'd drop the ObjectOfInterest and bind the Cube or Sphere directly to the Content property of the ContentPresenter from the combobox selection. In the ContentPresenter resources (Eg. <ContentPresenter.Resources>), define a DataTemplate for each object type. Do not define a Key for each template, only set that DataType property to the object types that will be displayed. In the DataTemplate, just define the Grid you want to display, complete with the databound controls. When you set the Content property of the ContentPresenter, it will automatically search its Resources and apply the appropriate template for each object.

If you need the ObjectOfInterest and that must be set as the content, then make sure there's a property you can access that identifies the object type. Create DataTemplates for the different object types, and define a custom DataTemplateSelector class that contains those templates and will check the object type property and return the appropriate DataTemplate. Then bind the DataTemplateSelector to the ContentPresenter.ContentTemplateSelector property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.