Posts Tagged property
How do I cancel a databinding property set in WPF?
Posted by Brian Seekford in WPF on November 30, 2011
I have a combobox in WPF that I am using the SelectedItem property with a DataBinding and sometimes certain items shouldn’t be selected based on certain criteria. Mine was that two comboboxes couldn’t have the same value.
Yes, there are other ways, such as removing invalid values from other boxes, but here was my solution for preventing the invalid property set by cancelling the selection/property set.
WPF sucks in this manner as there isn’t an easy way to update it. You can’t raise the property updated event since the binding ignores those when setting, to avoid recursion issue I suppose. (Yes, developers could defend by not raising the events if the property doesn’t change)
Anyway, the solution is simply to raise the property changed after the operation, by simply posting it to the work queue on the UI thread right afterward.
Code: in C#
public MyHeaderType SelectedHeader
{
get { return this.selectedHeader; }
set
{
bool resetMe= validateValue(value);
var origVal = selectedHeader;//store for reset
selectedHeader = value;
RaisePropertyChanged(() => SelectedHeader);
if (resetMe)//Why do I need to set the value above? No idea. Doesn’t work unless I do it that way. Yes. I tried.
{
Application.Current.Dispatcher.BeginInvoke( //force a reenter to put it back……
new Action(() =>
{
SelectedHeader = origVal;//put me back to the correct value.
}),
DispatcherPriority.ContextIdle,
null
);
}
}
}
Happy Coding!
Updated my WCF Data Contract but client doesn’t receive the property values….
Posted by Brian Seekford in .NET Development on July 15, 2011
I spent about ten minutes wondering why I wasn’t getting the data I expected from my WCF service. I had just changed my data contract to have one more property/field and the data wasn’t being received by my client application. The client is in javascript, so it’s happy with whatever it receives. So no errors there, just an undefined property.
I looked at fiddler and noticed it didn’t come through there either. Now I chalked it up to a stale build and rebuilt again. Hmm…. Still undefined…wtf?
Well, simple issue with a simple fix. It turns out I , DUH, forget to tag the property as a DataMember. So the serializer ignored it thinking it was just an internal variable. No need to serialize or expose. Geez.
My fault but it was pretty funny when I started going back through my entities and contracts and started comparing them.
So, in short, if your client for your WCF service is not receiving the property you expect on the data contract, make sure you marked them properly. Easy to overlook when you are updating the DAL DTO’s, Business Entities, and then your Contract Entity.
Example:
/// <summary>
/// Gets or sets the reference.
/// </summary>
/// <value>
/// The reference.
/// </value>
[DataMember(Name= "Reference")]
public string Reference { get; set; }
Happy coding!