Sean Cogan
Sean Cogan

Reputation: 2586

How do I make the Textbox component of a DatePicker read-only in Silverlight 5?

I am creating a simple application in Silverlight 5 to test another application's ability to pull data from it. The goal is to have as many input types as possible to test the other application's versatility. I am trying to add a DatePicker to a part of this form with the following code:

<sdk:DatePicker Grid.Row="3" Grid.Column="3" Height="25" Width="190" >
</sdk:DatePicker>

This works perfectly fine, but I want to make the TextBox component of the DatePicker read-only. The reason for this is that I want only valid dates to be entered into this text box, since I am then pulling the text from it and putting into a DataGrid. Any help would be appreciated! And if you need more information from me, just ask.

Upvotes: 0

Views: 1645

Answers (1)

Kevin Aenmey
Kevin Aenmey

Reputation: 13419

Create your own control that inherits from DatePicker:

using System.Windows.Controls.Primitives;

public class ReadonlyDatePicker : DatePicker
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var textBox = this.GetTemplateChild("TextBox") as DatePickerTextBox;
        textBox.IsReadOnly = true;
    }
}

Add the namespace to your project in your page:

xmlns:MyControls="clr-namespace:MyControlsNamespace"

Then reference your DatePicker using your namespace alias:

<MyControls:ReadonlyDatePicker Grid.Row="3" Grid.Column="3" Height="25" Width="190" >
</MyControls:ReadonlyDatePicker>

Upvotes: 2

Related Questions