This is a migrated thread and some comments may be shown as answers.

RadDatePicker in a DataTemplate

1 Answer 174 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 21 Oct 2010, 08:08 AM
I have a listbox that shows dates, when a user clicks on a specific date I want it to become editable. So when I click on the listbox item, a trigger switches its datatemplate from a template containing a textblock to a template containing a RadDatePicker.

When this occurs, the RadDatePicker immediatley updates the binding source with the value.  Why is this?  The WPF Toolkit date picker does not do this, but I'd have to use the RadDatePicker.

Because I am using entity framework self tracking entities, this immediatley marks the entity I've bound to as modified.

Does anyone know a way to stop the RadDatePicker updating the binding source as soon as it appears on a datatemplate?

I've recreated this problem in a test window.  The XAML looks like this:

<Window x:Class="RadDatePickerInTemplate.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="ViewTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Title}" />
                <TextBlock Text="{Binding Path=Date}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="EditTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBox Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <telerik:RadDatePicker SelectedDate="{Binding Path=Date, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </StackPanel>
        </DataTemplate>
        <Style x:Key="ContainerStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="ContentTemplate" Value="{StaticResource ViewTemplate}"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource EditTemplate}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="MyListBox"
                 IsSynchronizedWithCurrentItem="True" 
                 ItemContainerStyle="{StaticResource ContainerStyle}" />
    </Grid>
</Window>

... and the .cs looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
  
namespace RadDatePickerInTemplate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
  
            // Show a list of Dates
            MyListBox.ItemsSource = new List<ListItem>()
            {
                new ListItem() { Title = "Test 1", Date = new DateTime(2010, 3, 27) },
                new ListItem() { Title = "Test 2", Date = new DateTime(2010, 8, 19) },
                new ListItem() { Title = "Test 3", Date = new DateTime(2010, 2, 3) }
            };
        }
    }
  
    public class ListItem
    {
        public string Title { get; set; }
  
        private DateTime _date;
        public DateTime Date
        {
            get { return _date; }
            set
            {
                // Set a breakpoint here once the form is displayed, 
                // clicking a listbox row will immediatley break here
                _date = value;
            }
        }
    }
}

Thanks for any help!
Simon.

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 25 Oct 2010, 09:05 AM
Hello Simon,

Since the Q2.2010 release we have made major changes regarding the Date/Time pickers control. As a result the control has a new property SelectedValue, that must be used as it represent the SelectedDate merged with the SelectedValue. What you have to do is to replace the SelectedDate with SelectedValue property. Sorry for the misleading caused.

Kind regards,
Kaloyan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
DatePicker
Asked by
Simon
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or