Bind to property in code behind

May 13, 2009 at 3: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...

TIA

Code.....

XAML
<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 8:27 AM

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

May 15, 2009 at 8:49 AM

Hi,

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 12:26 PM

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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"                            
    xmlns:local="clr-namespace:FMS.Modules.Stock.Views"
    xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"

    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">
      <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <shared:DateTimePicker Value="{Binding EffectiveDate, ValidatesOnDataErrors=True}" FirstDayOfWeek="{Binding ElementName=SuppliersUC, Path=FirstDayOfWeek}" ></shared:DateTimePicker>
        </DataTemplate>
      </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
  </dg:DataGrid>
</commonViews:MaintenanceView>

 

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 2:44 PM

Hi,

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

http://msdn.microsoft.com/en-us/library/ms746659.aspx

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.

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples

 

May 15, 2009 at 4: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 5:05 PM

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

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples