Bind to property in code behind

May 13, 2009 at 2:31 PM

Hi All,

I am using the DateTimePicker in a UserControl and am trying to bind the FirstDayOfWeek property to a property in the code behind of the usercontrol, but just not managing to get it to work.

Please assist if someone can...



<shared:DateTimePicker Value="{Binding EffectiveDate, ValidatesOnDataErrors=True}" FirstDayOfWeek="{Binding ElementName=this, Path=FirstDayOfWeek}" ></shared:DateTimePicker>

Code Behind

public DayOfWeek FirstDayOfWeek
  get { return DayOfWeek.Monday; }

May 14, 2009 at 7:27 AM

Anyone? Phuleeeez guys. Need some assistance on this one.

May 15, 2009 at 7:49 AM


You are taking the right approach, however, there is a problem in your binding. Y9ou cannot use the 'this' keywords within XAML. ElementName bidings are used to bind to elements which have a name set via x:Name. To make your example work, give your UserControl a name using an x:Name attribute in teh XAML, then use that same name in your ElementName binding.

Hope that helps!

Regards,Colin E.

May 15, 2009 at 11:26 AM

Thanks for the tip ColinEver. It is not working though ):

Here is my code now...

UserControl XAML

<commonViews:MaintenanceView x:Class="FMS.Modules.Stock.Views.Suppliers"

    Height="Auto" Width="600" DataContextChanged="UserControl_DataContextChanged" Uid="Suppliers" x:Name="SuppliersUC" >
  <dg:DataGrid Name="pricesGrid" AutoGenerateColumns="False" HeadersVisibility="Column" SelectionUnit="Cell" SelectionMode="Extended" ClipboardCopyMode="IncludeHeader" GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False" VerticalScrollBarVisibility="Visible" Margin="5,0,0,0" Height="200">
    <dg:DataGridTemplateColumn Header="Efective Date" Width="150">
          <shared:DateTimePicker Value="{Binding EffectiveDate, ValidatesOnDataErrors=True}" FirstDayOfWeek="{Binding ElementName=SuppliersUC, Path=FirstDayOfWeek}" ></shared:DateTimePicker>


Code Behind
public DayOfWeek FirstDayOfWeek
  get { return DayOfWeek.Monday; }

Message in the output window
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=SuppliersUC'. BindingExpression:Path=FirstDayOfWeek; DataItem=null; target element is 'DateTimePicker' (Name=''); target property is 'FirstDayOfWeek' (type 'DayOfWeek')

The error in the output window is repeated for each row bound to the datagrid.

I included the datagrid code too incase it is something related to that that is causing this to not work.

Any other ideas?

May 15, 2009 at 1:44 PM


This looks like a XAML Namescope issue. Have a read of the namescope documentation:

Basically your template is in a different namescope to your user control, hence the named element cannot be found.

I would consider using a relative source binding in this case. Something like this ...

FirstDayOfWeek="{Binding Path=FirstDayOfWeek, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type commonViews:MaintenanceView}}}"

This navigates the visual tree looking for a class of a specific type, in this case your user control, using it as the source for the binding.

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples


May 15, 2009 at 3:24 PM

ColinEber: Thank-you man. Big time. Your solution worked 100%.

The time you spent helping me here is very highly appreciated!!

May 15, 2009 at 4:05 PM

Thanks for letting me know it worked. No problem :-)

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples