English 中文(简体)
PropertyValueEditor and DependencyObject in Blend 3 - Silverlight DesignTime support
原标题:

I m working on a set of controls that has a number of DependencyProperties. The properties are themselves DependencyObjects and created during the get method of the properties. During the Get method, they are also set back to the propertybag using the SetValue() method, so they are in fact valid in Xaml and their properties can be storyboarded without having to explicitly created in the the visual tree.

These DependencyObjects has all its properties as DependencyProperties as well, for supporting DataBinding. They are as mentioned above possible to use in Storyboards.

At the same time I m developing special designtime support for Blend 3 for these properties and have created an InlineEditorTemplate in the form of a Control. I create the template and set it for the PropertyValueEditor like this:

        var vectorEditControl = new FrameworkElementFactory(typeof (VectorEditorControl));
        var dataTemplate = new DataTemplate {VisualTree = vectorEditControl};
        InlineEditorTemplate = dataTemplate;

In the Control I have the following:

<Grid DataContext="{Binding Value}">

<StackPanel Orientation="Horizontal">
  <TextBox Text="{Binding Path=X, Mode=TwoWay}"/>
    <TextBox Text="{Binding Path=Y, Mode=TwoWay}"/>
    <TextBox Text="{Binding Path=Z, Mode=TwoWay}"/>
</StackPanel>

</Grid>

The editor shows up and I can edit the data. And even while debugging, I see that it actually sets the data back to the DependencyProperties on the DependencyObjects, but nothing happens to the Xaml. So the data is actually not persisted in any way in the Xaml and lost when I close the Xaml file and open it again.

Is there anything I need to do specifically for it to actually get into the Xaml? I was under the impression that this would happen automatically?

最佳回答

Excellent Question!

The core issue you re running into a misunderstanding as to what PropertyEditors in Blend/Cider end up databinding to.

Consider this object graph:
- MyControl
-- MyControl.MyProperty
--- FooClass
---- FooClass.BarProperty

Let s look at a scenario where we have a PropertyEditor (of any type: Inline, Dialog or Extended) to property MyControl.MyProperty. When inside MyPropertyPropertyEditor you d expect to get a fully settable copy of FooClass and be able to manipulate it s members. That s a good assumption, but the wrong one.

The core issue is that Blend/Cider have elaborate data structures that represent your model at design time. There s about 3-5 levels of abstraction in how Blend/Cider interact with an actual control.
Creating those levels of abstraction allows Expression Blend / Visual Studio designers to be leveraged between framewroks (Silverlight / WPF) and support advanced scenarios (like Property transactions and property chaining).

So, the value you actually get to DataBind to is just one of those levels of abstraction.
Don t believe me? In your custom PropertyEditor, register for this.DataContextChanged event and checkout the type in this.DataContext. You ll end up getting the PropertyValue class (or one of it s friends).

Every single property change you want persisted to XAML (and shown on the design surface) should go through those abstraction layers.

the question you have to ask yourself is "Where do I get one of these absteaction classes for my PropertyValue.Value property instance?".

Well, what I d do if I were you is create a ModelItem around MyControl.MyProperty and set that as your PropertyEditor.DataContext. We ve shipped an example of using ModelFactory.CreateItem in the Silverlight Toolkit as part of the Chart DefaultInitializer: Source Code, Ning Zhang (Awesome Design Time Dev) explains about ModelItem

If you ve got follow-up questions I d consider pinging PeteBl or UnniR through the Silverlight Insiders mailing list.

Sincerely,
-- Justin

问题回答

It partly solves my problem. I m having a dialog with UnniR for a followup. I couldn t see how I could use this together with the PropertyValueEditor, but for default values this is brilliant and something I ll implement ASAP.

Thanks.





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签