Is there an equivalent of DependencyObjectCollection<T> for WPF .NET4.0? - c#

The class allows the propagations of data context from the collection to its members. But it is only for SilverLight.
More info for DepedencyObjectCollection can be found from this post.
If the equivalent doesn't exist, what's the necessary steps required to create one?
I observe the desire behavior from the RowDefinitionCollection class. But I couldn't figure out its implementation.
thanks

The short answer is NO.
But I no longer think it is the correct implementation anyway. I end up using a collection of FrameworkElement and add them into the LogicalChildren of the custom control. By doing so, DataContext are passed on to the FrameworkElement naturally and everything works as expected.
It seems that that's how RowDefinition class is implemented in .NET 4.0. However, Silverlight version of RowDefinition is derived from DependencyObject directly. The MSDN document shows Silverlight documentation by default and misdirected me to the older and incorrect approach.

Related

Is there a way to build a DataTemplate with only C#

Is there a way to build a DataTemplate without using the deprecated FrameworkElementFactory or the XamlReader.Load method of string interpretation to code (related question) or (another related)?
DataTemplate dt = new DataTemplate();
StackPanel sp = new StackPanel();
ComboBox cellComboBox = new ComboBox() { Visibility = Visibility.Collapsed };
CheckBox cellCheckBox = new CheckBox() { Visibility = Visibility.Collapsed };
sp.Children.Add(cellComboBox);
sp.Children.Add(cellCheckBox);
// then add the stackpanel somehow?
Update: Why?
This is for a new program that may have to be supported for years. XamlReader string parsing at runtime for something that's got a constructor, methods, properties... feels kludgy. The FrameworkElementFactory has been deprecated for several versions but has never had a real replacement.
Is there a way to build a DataTemplate with only C#
Yes, you use either the FrameworkElementFactory or the XamlReader.Load method to create a DataTemplate programmatically.
...without using the deprecated FrameworkElementFactory or the XamlReader.Load method?
Short answer: No.
Why would you need yet another way when there are two already? Also note that it's perfectly fine to use the FrameworkElementFactory class if you don't want to use strings. Despite what the documentation on MSDN says, this type is not really marked as obsolete or deprecated in .NET Framework 4.7.2.
To elaborate on #mm8's answer: no, because if you inspect the FrameworkTemplate.LoadContent method you'll find that a template is basically a wrapper that uses either a FrameworkElementFactory or a TemplateContent to load the contents, TemplateContent being a utility to read XAML node stream. So these are the only two ways in which a template can operate.
Personally, of the two, I'd recommend using XAML (as a separate *.xaml file rather than a string), because it's far more readable, but also because FrameworkElementFactory has limited functionality, i.e. it only supports dependency properties and routed events (you cannot create a factory that would set a regular CLR property or event handler). It has one advantage over XAML though - it does support generic classes, so perhaps a mix of the two is the most proper approach.

Using Binding.IndexerName and Binding.ProvideValue in Xamarin Forms

I am trying to implement the answer in this so question
The problem is, that in xamarin forms 2 ingredients do not exist (or I have not found them yet):
Binding.IndexerName
Binding.ProvideValue()
I do not know why they do not exist. Maybe nobody has implemented them, maybe there is a technical reason why they cannot be implemented.
Can I still get the in xamarin forms?
Maybe in another way?
First, note that this answer probably doesn't work with Xamarin.Forms, or at least not with XamlC on.
If you want to get that working, your MarkupExtensions have to implement IMarkupExtension<BindingBase> instead of IMarkupExtension.
ProvideValue() is not defined in the Binding class, but in BindingExtension, but you probably won't win anything by instantiating a BindingExtension and calling ProvideValue on it versus returning the Binding directly.
The IndexerName refers to the IndexerName attribute of the Translator class. As you're not using it, the default is "Item", and you can use that hardcoded value.

WP7 - Data bound value in custom control, how to set default string so it's visible in designer

I'm fairly new to WP7 and totally new to Expression Blend.
I have a ListBox bound to a List of custom objects,
List<Person>
Each item in the list contains a custom control, MyControl which is bound to Person.
MyControl contains a TextBox which is bound to the Username property of Person.
All of this works fine. My question is: how do I set a default value for the TextBlock so that it becomes visible in the Designer or ExpressionBlend? With it being data bound, it has no text till it runs ... so I can't actually do any fancy styling using these wonderful tools unless I repeatedly delete the binding code to replace it with a string, make the changes, replace the binding code, repeat. Seems long winded!
Thanks,
Steven
What you want is "Design time data".
There are a number of ways of doing this. Fortunately there are also lots of resources online which explain it.
#Steven Have you looked at creating sample data in Blend to do what you require and then some binding to actually attached the data to the control bound to your list? You might like to check out Blend Sample Data as it guides you through a simple example of doing just that. You might then be able to adapt to to your own ends.
It depends if you are using any MVVM model or not.
My suggestion, if you are not using a MVVM, is to use Blend Sample data, is fast and quick.
If you are MVVM Light I've found very usefull to create two files:
DataService.cs - contains the real connection and data
DesignDataService.cs - contains the sample data
The two libraries are identical, from an call perspective so that in the ViewModelLocator you can swap them:
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
}
else
{
//SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
SimpleIoc.Default.Register<IDataService, DataService>();
}
In the Design class I've decided to create an XML file for each Model so that it's easy to change the sample data and test all possible scenarios.
I then use the Deserialize function to read it:
csNodeList _Copyrights = new csNodeList();
resource = System.Windows.Application.GetResourceStream(new Uri(#"Design/sampledata.xml", UriKind.Relative));
streamReader = new StreamReader(resource.Stream);
serializer = new XmlSerializer(typeof(csNodeList));
_Copyrights = (csNodeList)serializer.Deserialize(streamReader);
Please note that the file sampledata.xml has to be stored in folder Design and must be defined as Content not as Resource.
It is suggested to improve performance and load time.
M

UITypeEditor and readonly reference type properties in .Net, WinForms

I thought I had type editors and converters nailed until I tried to persist a Readonly Reference type property after editing it in a UITypeEditor.
In my UITypeEditor, because I'm working with a read only property, I'm careful to pass back the original value (after updating the relevant sub property).
This change is reflected immediately on the designer but will not be persisted unless I do something like resize the control that the property is attached to.
To fix this I, blindly, include a call to context.OnComponentChanged() before returning the value.
I can see why this is needed. It's a reference type, I've altered it (not replaced it), and the property grid doesn't know this. I have a couple of questions for clarification:
Do I need a call to context.OnComponentChanging as well? A simple call to OnComponentChanged works in the tests I've done so far, but I don't want biting on the arse at some point in the future.
Also, is there any danger that, with my call to OnComponentChanging, I'll be persisting other components, in DesignerTransactions, that I shouldn't be persisting?

User Control Property Designer Properties

For a C# UserControl on Windows Mobile (though please answer if you know it for full Windows...it might work) how do you change what shows up in the Designer Properties window for one of the Control's public Properties. For example:
private Color blah = Color.Black;
public Color Blah
{
get { return this.blah; }
set { this.blah = value; }
}
This shows up for the control, but it's in the "Misc" category and has no description or default value. I've tried using the settings in System.ComponentModel like "DesignerCategory", such as:
[DesignerCategory("Custom")]
But says this is only valid for class declarations... could've sworn it was the System.ComponentModel items I used before...
Update:
#John said:
DesignerCatogy is used to say if the
class is a form, component etc.
Try this:
[Category("Custom")]
Is there a particular namespace I need to use in order to get those?
I've tried those exactly and the compiler doesn't recognize them.
In .NETCF all I seem to have available from System.ComponentModel is:
DataObject,
DataObjectMethod,
DefaultValue,
DesignerCategory,
DesignTimeVisible,
EditorBrowsable
The only one it doesn't scream at is EditorBrowsable
DesignerCategory is used to say if the class is a form, component etc.
For full windows the attribute you want is:
[System.ComponentModel.Category("Custom")]
and for the description you can use [System.ComponentModel.Description("This is the description")]
To use both together:
[System.ComponentModel.Category("Custom"),System.ComponentModel.Description("This is the description")]
However this is part of system.dll which may be different for windows mobile.
Is this of use to you? I am not into CF development, but it looks like you need to add some XML metadata to enable it:
http://blogs.msdn.com/bluecollar/archive/2007/02/08/adding-compact-framework-design-time-attributes-or-more-fun-with-textboxes.aspx
Interesting read.. Looks like a lot of design time support was stripped out of CF because you dont design them on the devices.. Which seems kinda weird to me.. Cant imagine using a handheld as a development rig!
Scroll down about half way for the good stuff ;)
The article does not suggest that anyone is designing ON the device. However, when you create a Compact Framework project, the compact framework (for your desktop PC) is used to handle design time rendering. If you think about it that is what you expect. The same framework (or nearly so) is used to do the rendering both on your PC at design time and later on the device at runtime. The issue is that the design time attributes were not added to the compact framework (I assume to reduce the size).

Categories

Resources