A Quick Lesson On Simple Yet Confusing Depedency Property Issue

Filed in Software Developement Leave a comment

This is an issue that I just ran into and was a little confused as to why it was happening, but upon thinking about it for a moment it makes perfect sense.

When you are creating your DP’s, you have the option of specifying a default (initial) value. The problem with this value is that it is created and stored statically. So if you are creating a DP for, say, a collection, and you initialize it statically in the DP definition then your collection reference now exists statically. So regardless of whether or not you create ten instances of your DO, they will all contain the same initial value because they will all point to the same static reference.

So the conclusion to this quick lesson is that you should never initialize your DP’s with reference types, always perform this step at instantiation to guarantee unique DP values per object instance (unless of course you want to share the reference). As for value types it doesn’t matter nearly as much because the value is copied anyways.

, ,

Anonymous Type Return Value in C# 4.0

Filed in Software Developement | Uncategorized Leave a comment

Ever wanted to return an anonymous type from a method or property, but then find out you can’t use it because there is no valid return type?  Well I have, and I will explain how to get around this issue.

The solution as you may have guessed is to use dynamic types, which allow you to build the type definition at run time and return it.  Since there is no static compilation type checking, it doesn’t care that you can’t define the return type of your method or property.  This isn’t something very common, or even recommended in most situations, but it can come in handy in a pinch.  Here is some totally useless, but good sample code explanation.

private bool flagValue = false;
private bool isFlagValueSet = false;

private dynamic Flag
{
    get
    {
        return new { Value = flagValue, IsValueSet = isFlagValueSet };
    }
}

public bool Test()
{
    return Flag.IsValueSet ? Flag.Value : false;
}

All you gotta do is ensure that you reference the Microsoft.CSharp.dll to get access to dynamic types.  You won’t get any intellisense on the flag variable, but that’s because the type information is computed at runtime only.

,

Simulating Multi-Source One Way Data Bindings

Filed in Software Developement Leave a comment

The title is a bit of a mouthful, so here is the quick synopsis.  We have one to many controls that drive a single view.  Each control implements the same interface, something that provides the view with its context.  That way regardless of what control is used, the single view updates appropriately.

The problem here is that we need to bind the DP of the view’s context to multiple sources, something which a standard Binding does not allow.  MultiBinding allows multiple sources, but it would be impossible to know which source was actually “made active” most recently, so we could never return the right source accurately (unless we gave context to the sources themselves, which would be poor design).

The solution to this problem is rather simple, we just need to manage that multi-source aspect of the binding manually in the location where the binding is made or where the DP’s live (this could be the same place if you are doing MVVM).  First create a read only DP that will act as your view driving DP, it can be initialized to null.  Now create a standard event handler function to handle the property changed event for the driving DP on each source.  Hook up the DP value changed events:

private void ViewContext_PropertyChanged(object sender, EventArgs e) 
{
    SetValue(ViewContextPropertyKey, (sender as IViewContextProvider).ViewContext);
}
DependencyPropertyDescriptor.FromProperty(Source1ViewModel.ViewContextProperty, typeof(Source1ViewModel))
    .AddValueChanged(m_Source1ViewModel, ViewContext_PropertyChanged);

In the event handler, we simply update your RO DP and that’s it!

If the scenario seems a little obscure, think of this situation.  You want to write a file explorer type of view, the explorer will obviously display files and folders in some sort of list or grid.  On the left hand side, you have multiple panels that determine the context of the file explorer.  One panel is your local system, another panel is a local search, another panel is an FTP site, etc…  All these left side panels can drive your single view through this RO DP pattern.

, ,

TOP