.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Dependency Properties get/set method question

Posted By:      Posted Date: September 28, 2010    Points: 0   Category :WPF


I recently added a new dependency property to a control of mine and bound its value to my control's ViewModel.  I put custom code in the Get method of the dependency property and saw the return value from that method does not come across to the ViewModel.  After some research, it seems that when the XAML parser attempts to get or set this property's value that it does not use these get/set methods at all.  So to make this DP useful for my scenario, I have to figure out another way to set this value ahead of time, i.e. through code.

My question is, what is the point of these get/set methods for your DP if the XAML parser isn't going to use them?  Is it really all that useful to use these in code?  I would imagine DP's are mostly being used for the purpose of data binding in which case it'd be helpful to be able to define how WPF will get and set the value.  I liken this scenario to a public property with a private backing field, and having WinForms or whatever just directly access the backing field and skipping my property's getter/setter, which would obviously be a bug.

I'd appreciate any insight on this.

View Complete Post

More Related Resource Links

Dependency Properties

When you begin to develop appliations with WPF, you will soon stumble across DependencyProperties. They look quite similar to normal .NET properties, but the concept behind is much more complex and powerful

Question in Get method in C#


I got a recent working code from client.I see errors in the get methods when i compile , below are some place where the error happens.Can any one help me out?


Error 2 'System.Environment.NewLine.get': cannot explicitly call operator or accessor C:\Documents and Settings\shankar\My Documents\Visual Studio 2008\Projects\DKCLwin\DKCLwin\LocalizationAdmin.cs 150 170 DKCLwin

Error 2 'System.Environment.NewLine.get': cannot explicitly call operator or accessor C:\Documents and Settings\shankar\My Documents\Visual Studio 2008\Projects\DKCLwin\DKCLwin\LocalizationAdmin.cs 150 170

Foundations: Dependency Properties And Notifications


Windows Presentation Foundation dependency properties don't always play well with others. Learn how you can compensate for their lack of notification events.

Charles Petzold

MSDN Magazine September 2008

C# Properties question

Hello, I have a class that contains several properties. This class is serialized to an XML file. The properties are: TestA, TestB, TestC, TestD. Can I make some code that allows me to: Serialize TestA and TestB to the XML file if condition X is met and serialize TestC and TestD to the XML file if condition Y is met? Right now, all properties are serialized to the XML file and that's not what I want. Thanks in advance!

How do I bind these dependency properties?


I have two custom classes: "Foo" with a dependency property called "FooValue", and "Bar" with a dependency property called "BarValue". I can declare these in Xaml and bind them the usual way like so:

	<my:Foo x:Name="foo" x:Key="foo" FooValue="Hello World!">
	<my:Bar x:Key="bar" BarValue="{Binding Source={StaticResource foo},Path=FooValue}"/>

But what if Bar is a member of Foo? How do I do the binding then?

<my:Foo x:Name="foo" x:Key="foo" FooValue="Hello World!">
		<my:Bar BarValue="{Binding *** what goes here? ***}"/>

I can't bind to the "foo" key because the parent object is still in the process of being created and doesn't exist yet. I've also tried using FindAncestor but that doesn't seem to work with custom classes. What's the mechanism that controls use to get it to work?

Reflection question - how to tell if a method is defined in an interface?


OK so the Reflection namespace has tools so you can tell if a method has a base definition in a class (MethodInfo.GetBaseDefinition().DeclaringType). This way for instance I can query the ToString method of my favorite class and see that yes, indeed this inherits from System.Object.

I also can detect if a method is an explicit interface implementation, by checking the name of the method (methods representing explicit interface implementation will have the fully qualified name of the interface pre-pended to the value of the Name property) so I'm okay there.

But what about plain ole interface implementations? How do I get this info?

Thanks if you can help!

Reflection question: PropertyInfo properties seem in an inconsistent state



Noticed something odd here... In class System.Collections.CollectionBase there is a property called InnerList

(see http://msdn.microsoft.com/en-us/library/system.collections.collectionbase.innerlist%28v=VS.90%29.aspx)

From the msdn link you can see that this property is read-only, as there is only a "get" and no "set". In the reflections namespace there is a class called PropertyInfo. When I query the PropertyInfo object corresponding to this InnerList property I get CanRead = true and CanWrite = false (as expected).

BUT when I go to retrieve the get method (with GetGetMethod) I get null. How is this possible, what does this mean?

Thanks in advance,

Reflection question - how to tell which exceptions a method throws


I've found an ExceptionHandlingClauses collection in MethodBody but I'm looking for the exceptions that are *thrown*. You know, when you look at the ReadByte method of FileStream on msdn and you see that the ReadByte method throws two exceptions - the NotSupportedException and the ObjectDisposedException. 

How can I get this info programatically?


Revised Question XML Value Method Return Multiple Rows


Question earlier asked and answered to run query to collect sql data from xml schema

was able to collect the scheme from node but query did not return data but xml string


I ran this query

select x.col.value ('




cross apply



passing values to object properties of action method parameter


I have an action method that accepts a single parm.

That parm is an object with multiple properties. MVC correctly maps values from the querystring
to properties in the parm object by matching property name to querystring value name. 
Problem is, properties in the action method parm object which are themselves objects with
properties, are not assigned their matching values from the querystring. The class object has a
constructor which accepts a single string parameter.

How do I assign to properties of an action method parameter which are themselves class objects?



                <%: Html.ActionLink("Edit", "ProductMasterEdit", 
                       new { ProdKey=item.ProductKey.Value, 
                       ProductKey = item.ProductKey.Value })%> 

    public ViewResult ProductMasterEdit(ProductMaster Item)

  public partial class ProductMaster

    public ProductKey ProductKey
      get { return new ProductKey(this.ProdKey); }
      set { this.ProdKey = value.Value; }


Question about GroupPrincipal.FindByIdentity(PrincipalContext, String) Method and AD account permiss



I have an ASP.NET application that uses System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity (PrincipalContext, String) method to get a group principal object.  When the code runs under an AD account that has the "Read All Properties" permission for an AD global security group then the corresponding group principal object can be instantiated without problems.  However, this "Read All Properties" permission consists of fifty or so more detailed permissions to read properties like "Name", "Description", "msExchLabeledURI" and so on.  Some of these properties can be locked down for some AD accounts and I found that GroupPrincipal.FindByIdentity method may return a NULL in such cases. Is there a minimum set of group properties an AD account should have the Read permission to so the FindByIdentity method could instantiate the corresponding GroupPrincipal object?

Thanks in advance!


How and When Dependency Properties are bound and evaluated?



I have a custom combo box control derived from existing WPF combo box. This custom combo has a DP called AssociatedEnum, once set it evaluates the enum menbers and adds them to Items collection.. However all this enumerating over available values, is done in Combo's loaded event Is it reasonable to assume that all the bindings on Dependency props will be set and evaluated to their initial values during the controls load event.

I am not very sure about this as i have seen some instances where the Binding is evaluated post load event, which defeats entire purpose of writing the selection in load event. My question should i go ahead and implement property changed handler for this dependency property??

Regards, v703218

link | edit |

Can you add Dependency Properties to custom activities?


I'm trying to add a simple DependencyProperty to a base activity of mine that inherits from NativeActivity.  Is this possible?  It doesn't seem so since the SetValue and GetValue methods of the dependency property definition are not recognized.

I want a dependency property because I'd like to be able to check the property from a Trigger in the activity designer ... like this:

            <Trigger Property="{Binding Path=ModelItem.IsEnabled}" Value="True">
              <Setter TargetName="Header" Property="<

Question on WorkflowApplication class design ( Func Properties)





I found out that the Workflow Application class offers Func<T> properties to inject code for example when a error occurs.

whats the reason behind the Design Decission of giving the WorkFlow Foundation class Func<T> properties instead of event?


For Example


 Func<WorkflowApplicationUnhandledExceptionEventArgs, UnhandledExceptionAction> OnUnhandledException { get

; set

; }





 Action<WorkflowApplicationCompletedEventArgs> Completed { get

; set

; }


Is it just to prevent the multiple receipients ?

Or is it related to memory mangement due to fix pins caused by eventhandlers ? To be honest i must confess that i'm not sure wether Func<T> will created a fix pin in memory :-(

I'm not sure about if this is good or not, but it would be very nice to get knowledge of the reason why this class is event

Parsing C# Classes, Methods, and Method Properties Using Regex's


I have a web project that allows multiple developers to contribute to a .cs file in the App_Code folder. I need to dynamically report on which classes exist in the file, which methods are in those classes, which properties are expected by those methods, and what the method returns. I can't seem to get my curly brace matching to work using regex's.

I went down the road of trying to dynamically build the code and then use reflection to enumerate the objects, but I don't think that's going to work.

Any help would be appreciated.

Separation / Cyclic Dependency Question



I hope this is both the correct forum, and that maybe someone can offer some information on my question.

I've been building up a series of libraries that are used within the applications we develop at work, I've worked hard to separate them based on the following structure:

  • UI (typically the web based application)
  • Business (the entities, Patient, Ward, Hospital etc)
  • DataAccess (a set of DataServices which get/set data to the data source)
  • DataSource (typically SQL server)

The problem I initially encountered was that I was creating say the following;

  • myCompany.myApp.Business
  • myCompany.myApp.Common
  • myCompany.myApp.DataAccess

for an application, and more often than not we would also need a web service.  The web service had its own little set of files for handling the getting/setting of the data.

To me this felt wrong.  Surely if I have a DataAccess library specifically for the getting/setting of data which resides in between the Business and DataSource layers I should make every attempt to use it for the WebService too - why re-invent the wheel etc?

Without going into all the detail, the structure of these services were almost identical, aside from one made calls to stored procedures, and one made calls to web services.  I took each

Properties Window Question for Visual Studio 2010

For Visual Studio 2010, how do you change the writing of the properties window categories like accessibility, appearance and behavior?  It is a grey foreground and grey background.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend