WPF Binding, null value

Posts   
 
    
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 05-Aug-2010 16:38:36   

I have nullable varchar FK column in DB, named let's say CodeType.

nullable types are turned off in llbl project.

I have usercontrol, with DependencyProperty SelectedValuePk.

I binded the value to CodeType FK field. The SelectedValuePk returns null when nothing's selected. But when I try to save the entity, ORMQueryException is thrown, because CodeType is String.Empty, not null, therefore the fk reference conflicts. I understand why.

I there any workaround, how to make such binding?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 06-Aug-2010 11:24:39   

If the bound control stores a "" instead of NULL, you've to check whether you can configure the control in such a way that "" is NULL. I don't know if you use a grid or something, most grids have these kind of settings. Can you change the value in an eventhandler? The entity has a preprocess override, which you might want to look into but I need to know what you're using: v3, v2.6, selfservicing / adapter?

Frans Bouma | Lead developer LLBLGen Pro
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 06-Aug-2010 14:58:33   

Sorry, i'm using v2.6, selfservicing.

I have my own usercontrol, so it is no problem to provide "" instead of null, but wpf binding can use converter, which seems to be better for this case. If I set "" to field mapped on varchar null column, it is the same as if i would write SetNewFieldValue(fieldIndex, null); ? I don't think so.

in xaml I have control used like this:

<c:TableDropDown x:Name="tblRunMode" SelectedValuePk="{Binding CodeEventRunMode, UpdateSourceTrigger=Explicit, Mode=TwoWay}" EntityName="EventRunModeEntity" Grid.Row="3" />

UpdateSourceTrigger=Explicit means I have to call BindingExpression.UpdateSource to update the entity, so it's not updated automatically. No problem to use converter there to conver null value to "". I want to avoid writing some special code everytime I use this control.

Inside the usercontrol I have binded TableDropDown.SelectedValuePkProperty to ComboBox.SelectedValueProperty, with EntityToPKConverter (as combobox has entitycollection as ItemSource).

SelectedValuePkProperty is DependecyProperty what allow to define PropertyChangedCallback and CoerceValueCallback. CoerceValueCallback is something like setter in CLR property, but it is static method with reference to DependencyObject in method parameter.

The binding as show above is described in every WPF tutorial as best practice or maybe the only good practice. One of the ideas is not to write eventhandlers, but xaml

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Aug-2010 18:53:22   

Following the Frans suggestion, Did you already try the TargetNullValue property?

<c:TableDropDown 
     x:Name="tblRunMode" 
     SelectedValuePk="{  Binding CodeEventRunMode, 
                                    UpdateSourceTrigger=Explicit, 
                                    Mode=TwoWay, 
                                    TargetNullValue={x:Static sys:String.Empty} 
                                 }"                 
     EntityName="EventRunModeEntity" Grid.Row="3" 
/>

If you still have problems, could you please attach a tiny repro solution with your user control so we can look at it?

David Elizondo | LLBLGen Support Team
Liero
User
Posts: 40
Joined: 18-Sep-2009
# Posted on: 09-Aug-2010 10:46:45   

Seems to be working for me. Thanks