Binding ModelVisual3D children to an Observable collection in WPF? - c#

Not sure if I'm going about this the wrong way, but I have an application where a user creates various cuboid objects of different dimensions and positions. These objects are displayed in a Viewport3D and the user can add/edit/delete them and can move the object by doing a drag/drop which should update the underlying data. The cuboids are defined relative to some base coordinate system (meaning that I have nested cuboids where the local coordinates are relative to some parent object). The data itself needs to be persisted for later re-use. I'm currently storing the data in an observable collection that I serialize. However, what I'm not sure is how to dynamically create a ModelVisual3D and have it be bound to a list object that contains a MatrixTransform3D which defines those properties. A DataTemplate does not appear to be usable on a ModelVisual3D or the Children because it is a VisualCollection and not an ItemsControl object. I'm not sure if I'm thinking of this the wrong way and if so, any suggestions would be appreciated. Can this be done in XAML or is this all code-behind? I've seen a possible code-behind solution, but is there a way to take advantage of letting the XAML do it? --> How can I use databinding for 3D elements like Visual3D or UIElement3D

Related

Iterating XAML elements from within a file

I have an application that loads a XAML file, and shows it in a ContentControl. What I also want to implement is a way to go over the XAML code that was loaded to the control and populate a listbox with the names of the Canvases I find.
The main task of this tiny application is to enable the user to change colors of specific canvases and their elements. Meaning that I have, for example 10 canvases, each containing some <Path> elements. I want to give the user a list of the canvases that were found in the XAML code, and for each canvas the user will be able to change its fill color (for example), if the user changes the fill color of a specific canvas, the child elements of that canvas will change their "Fill" attribute to the selected color as well.
I didn't find a way to iterate through the Canvases or the Path elements, so if anyone have a way to do this I would be glad to hear.
Thanks!
You seem to need to traverse the logical children tree of a given control. For this, you need to use LogicalChildren property recursively.
You can find more information on the element trees in WPF here.

Need design advice for custom list control in .NET Windows Forms

First of all, here's some concept art for how this custom list control must look:
http://img816.imageshack.us/img816/1088/customlistctrl.png
Each list item is fairly complex and turns into an "edit interface" when the mouse hovers over it. I also have PNG image files for every skinning detail of this thing, including the scroll bar.
What is the best approach to get started implementing this? Would I create a custom control and simply render all of this in GDI?
Could I make the list control a transparent clip region with a scroll bar? For the individual list items, would I simply use a textured panel as the backdrop for each item and place existing .NET forms (like combo boxes, buttons, edit fields, etc) as children of that?
I've never had to create something this detailed before.
If you want your control to look exactly like the given picture (which is nice), you will end up drawing much of it, if not all of it, yourself. One possibility is to subclass each control being used and override the OnPaint method to do your custom drawing. This assumes a design where everything in your picture is an individual control.
I myself might make each row a separate UserControl-derived class, perhaps with an internal constructor so users of your control can't create the row directly. Within your SkinnedListRow class (or whatever name), you could have each of the subcontrols. By the looks of things, the row contains three controls that display numbers and one that displays any kind of text.
For the editing portion, derive another UserControl that contains all the controls you picture. Both the display controls and the editing control are owned by the SkinnedListRow from above, so it knows how to load data from one set into another.
You have a good amount of work in front of you, but your idea looks nice. Good luck.

A tree-like, manipulable control for manifold hierarchical data

I am new to C# and WPF and I need to create a diagramming control similar to MS Visio but without direct user interaction. I have tree-like hierarchical data whose nodes can be of the same type. Quite like graphs whose vertices themselves contain graphs (i.e. vertices are subgraphs). I cannot use the Graph# library for several reasons.
The main requirements for my (customized) control are:
free moveable/draggable items (which represent hierarchical data)
items should have a box layout
items can be connected by arrows
Unfortunately, I don't know how to start. I tried several approaches like nesting ListBoxes but each approach had disadvantages. What do you think about nesting TreeViews and templating them? I need something like a Canvas container to freely position my items.
Do you have any suggestions? Thank you very much in advance.
If you need free movable/draggable items you probably need a degree of flexibility that built-in controls don't really provide. You're better off constructing your own controls, perhaps deriving from existing controls that provide close-enough functionality.
So for starters you'd have a control for a box item and a control for an arrow. The controls need to be movable and draggable inside a container, so you'll also need a container, probably derived from Canvas.
What I'd really recommend is to have a look at Model-View-ViewModel techniques; they can be daunting for a beginner but the benefits are immense. In this case, you'd have a collection of objects that represent your structure; all the objects will have, for starters, X and Y properties that define their coordinates. The objects are represented on the page by the previously mentioned controls, with bindings from their relevant properties to the properties of the objects. The objects are the ViewModel, and the controls represent the View.
Then, when you move the items with the mouse (the code for that is pretty easy), the properties of both the controls and the objects are automatically updated. More properties for the objects can include size, labels, and a collection of snap points that represent where arrows can connect to the object.
Arrows can be represented with the same concepts, except that instead of explicit X and Y coordinates they would have associated objects and snap points; then their coordinates would be automatically calculated based on the positions of the snap points.
Based on these structures, you can do actions exclusively from the ViewModel, like adding new items automatically connected to their parents, and have the view update based on that.
This is quite a broad topic so I really should stop now; I don't even what articles to recommend to get you started. Probably anything by Josh Smith on the topic of MVVM :P

How can I design a curve editor that works well with the WPF model?

I have a data model that represents segments of a curve that looks like this:
Envelope curve http://img710.imageshack.us/img710/9059/envelope.png
The fields are x and y coordinates of the anchor points (red), and a slope value represented by the control points (green). For the curve to be editable by the user, I'm thinking of using Thumb objects for both anchor and control points.
Since I also would like to be able to present the user with an interface where it is possible to enter coordinates into text boxes, I'm thinking of implementing this curve editor as a lookless control. The problem I face is to find the best way to organise the elements. Some thoughts on what I could do:
Add the data model view objects to an ItemsContainer with a custom ItemsPanel and a DataTemplate that creates one anchor point and one control point for each data model object. This causes two and two points to be wrapped in a ContentPresenter which makes the interaction between the custom panel and the thumb points difficult.
Use two different data model view objects, one for anchor points and one for control points, and a DataTemplateSelector to select the right DataTemplate. While there still is a ContentPresenter between the item and the panel (is it possible to avoid this?), interaction may be easier when there is only one item in the ContentPresenter.
Don't use ItemsContainer at all, instead add the Thumb objects directly to the visual and logical trees of the custom panel. Of course I loose a lot of flexibility when it comes to the look and feel of the Thumb objects when they are not specified by templates. Maybe it's possible to duplicate this functionality from ItemsContainer so my control can have separate DataTemplates for the anchor and control points. The Thumb objects could then be added directly to the panel without ContentPresenters.
What's the most reasonable way to do this? Are there other alternatives than the ones I've thought of?

Passing the current mouse position to a ViewModel?

In my MVVM application, I have a Direct3d render window that shows a bunch of 3d meshes in a scene. In that render window, I want to be able to click on one of those 3d meshes in the scene and move it around, having it follow the mouse cursor. This is typical 3d editor stuff, moving a mesh along in screen space. So I need to be able to get the current mouse position, preferably relative to that Direct3d render window.
What's a method to do that?
Thanks!
Edit: Changing the wording since it was too generic and led to confusion.
InputManager.Current.PrimaryMouseDevice is probably your best bet.
Wrap it up in an interface that exposes the info you need and inject it using your favorite IoC framework.
I think this is the wrong way to think about the MVVM pattern. Let me illustrate this by changing your question slightly.
If you have some custom text editing control, what would be the best way to pass keystroke events to the ViewModel?
Wouldn't it be better to bind the the data to the control and then update the data through the binding as the control is manipulated?
So, you have a list of objects you want to show in a 3D view? Pass the objects as a they are, and use template binding to bind each object type to a DataTemplate describing the 3D object, which includes bindings to the X,Y,Z items in the object.
I would declare a delegate on the viewmodel and make the view register to it. this way the viewModel doesn't need to know about the view and is easy to unit test.

Categories

Resources