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

maskedtextBox Mask

8 Answers 224 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 06 Jun 2011, 02:15 PM
I would like the mask of a maskedtextbox to be bound to a dropdown or other on the viewmodel parent in a gridview.

I need to be able to switch between hh-mm-ss input and mm-ss input.

How do I implement this??

8 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 09 Jun 2011, 02:55 PM
Hello Michael,

We need some more info in order to advice you better. You could elaborate more on the structure of your ViewModels and their properties.Thank you in advance for your cooperation.

Kind regards,
Petar Mladenov
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
0
Michael
Top achievements
Rank 1
answered on 10 Jun 2011, 08:15 AM
OK

in my view I currently have

                                  
<telerik:GridViewMaskedTextBoxColumn
    DataMemberBinding="{Binding rTime}"
    Header="Stop Watch Time"
    IsVisible="{Binding ElementName=tbStopWatch, Path=IsChecked}"
    MaskType="DateTime"
    Mask="hh-mm-ss" />


then the viewmodel exposes a database Timespan as a string such as

public string rTime
{
    get
    {
        //timespan & format
        TimeSpan watchTime = TimeSpan.Parse(_Result.rTime);
        return string.Format("{0:hh\\:mm\\:ss}", watchTime);
    }
    set
    {
        try
        {
            DateTime dt = DateTime.Parse(value);
 
            //check if within first hour (Date Time)
            if (dt.Hour == 12 || dt.Hour == 0)
            {
                var time = string.Format("{0:mm:ss}", dt);
                _Result.rTime = String.Concat("00:" + time);
            }
            else
            {
                _Result.rTime = string.Format("{0:hh:mm:ss}", dt);
            }
 
        }
        catch (Exception)
        {
 
        }
    }
}

 
I want to switch the mask between accepting only min/sec for 90% of the time but being able to switch to hh-mm-ss if required?
0
Alex Fidanov
Telerik team
answered on 14 Jun 2011, 04:32 PM
Hello Michael,

I can see that you are binding to a string property and using a GridViewMaskedColumn. You have set the MaskType to DateTime and using a date time mask. The RadMaskedTextBox used in that column would expect a DateTime object instead of a string. However, you would not be able to bind the Mask property of the GridViewMaskedTextBoxColumn, because it is not a dependency property.

What you can do is to use the Cell(Edit)Template of the column and create a template with a RadMaskedTextBox control and bind the Mask and the Value properties of the control.

<DataTemplate>
     <telerik:RadMaskedTextBox Value="{Binding Date, Mode=TwoWay}"
                               Mask="{Binding Path=Mask}"
                               MaskType="DateTime"/>
 </DataTemplate>


Regards,
Alex Fidanov
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
0
Michael
Top achievements
Rank 1
answered on 15 Jun 2011, 02:34 AM
is there an example of how to use templating for the gridview?
0
Michael
Top achievements
Rank 1
answered on 15 Jun 2011, 03:05 AM
so i have this:
<telerik:GridViewMaskedTextBoxColumn
                                       DataMemberBinding="{Binding rTime}"
                                       Header="Stop Watch Time"
                                       IsVisible="{Binding ElementName=tbStopWatch, Path=IsChecked}"
                                       MaskType="DateTime"
                                       Mask="hh-mm-ss">
                                           <telerik:GridViewMaskedTextBoxColumn.CellEditTemplate>
                                               <DataTemplate>
                                                   <telerik:RadMaskedTextBox Value="{Binding rTime, Mode=TwoWay}"                                                   
                                                  Mask="{Binding Path=HourMask}"
                                                  MaskType="DateTime"/>
                                               </DataTemplate>
                                           </telerik:GridViewMaskedTextBoxColumn.CellEditTemplate>
                                       </telerik:GridViewMaskedTextBoxColumn>


and now the viewmodel shows:

public string HourMask
{
    get
    {
        return _hourMask ? "hh-mm-ss" : "mm-ss";
    }
}

but when i edit theres no mask at all?

actually taht viewmodel is a level above what the rest of the grid is referencing...how do i reference the "outer Viewmodel?"
0
Alex Fidanov
Telerik team
answered on 20 Jun 2011, 09:12 AM
Hi Michael,

You could bind the Mask to the outer ViewModel (which I suppose is the DataContext of the RadGridView) with RelativeSource binding (AncestorType = RadGridView). For example:
<telerik:RadMaskedTextBox Mask="{Binding Path=DataContext.Mask, RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}}"
                                                     Value="{Binding MyProperty, Mode=TwoWay}"
                                                     MaskType="Numeric"/>


All the best,
Alex Fidanov
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
0
Michael
Top achievements
Rank 1
answered on 21 Jun 2011, 12:55 AM
My solution was to add the parent Viewmodel to the constructor of the results viewmodel. So my Viewmodel now has...

public string HourMask
{
    get
    {
        return _parentViewModel.HourMask;
    }
}


and in the parent
public string HourMask
{
    get
    {
        return _hourMask ? "hh-mm-ss" : "mm-ss";
    }
}

and so in the view:
<telerik:GridViewMaskedTextBoxColumn
DataMemberBinding="{Binding rTime}"
Header="Stop Watch Time"
IsVisible="{Binding ElementName=tbStopWatch, Path=IsChecked}"
MaskType="DateTime"
Mask="hh-mm-ss">
    <telerik:GridViewMaskedTextBoxColumn.CellEditTemplate>
        <DataTemplate>
            <telerik:RadMaskedTextBox Value="{Binding rTime, Mode=TwoWay}"                                                   
           Mask="{Binding Path=HourMask}"
           MaskType="DateTime"/>
        </DataTemplate>
    </telerik:GridViewMaskedTextBoxColumn.CellEditTemplate>
</telerik:GridViewMaskedTextBoxColumn>


however since making these changes the edit field has been behaving quite eratically. When entering a number pressing - which normally moves form entering minutes  to seconds in the mm-ss mask now just returns to mm in the first instance THEN moves to seconds on the 2nd press and if i dont press it after that it will now let me enter a number to the minutes columns??
0
Tina Stancheva
Telerik team
answered on 23 Jun 2011, 03:33 PM
Hello Michael,

Can you please send us a sample project illustrating the issue because I cannot reproduce it on our side. And I am not sure I understand entirely the ViewModel logic from the code snippets you provided. And the cause for the issue might lay there.

Thank you in advance for your cooperation.

All the best,
Tina Stancheva
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
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Michael
Top achievements
Rank 1
Alex Fidanov
Telerik team
Tina Stancheva
Telerik team
Share this question
or