Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Display Count from textbox - MVVM
Message
From
25/10/2011 14:20:32
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Application:
Desktop
Miscellaneous
Thread ID:
01527088
Message ID:
01527330
Views:
52
>>I have a WPF application with MVVM and need to show a label over a textbox that indicates the remaining number of characters allowed to be in the textbox. I can't seem to wrap my head around the best way to do this.
>>
>>The user is able to type up to 183 characters in the text box. I would like to show a label above it that indicates (Max Characters: 183). But as they type, I want to show (Remaining Characters: 182). Once they reach 0 it should be 0 but then show in red.
>>
>>The text box is bound to a property in the View Model which I could get a count on easy enough, however how would I raise the event indicating the text property has changed? WPF does not access the property setters directly.
>
>I'm sure you've implemented this by now but here's one solution:
<Window x:Class="WpfApplication4.MainWindow"
>        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>        xmlns:local="clr-namespace:WpfApplication4"
>        Title="MainWindow" Height="350" Width="525"
>        >
>    <Window.Resources>
>         <local:ViewModel x:Key="VM"/>
>    </Window.Resources>
>    <StackPanel DataContext="{StaticResource VM}">
>         <TextBox x:Name="txtBox" Text="{Binding Path=TheText, UpdateSourceTrigger=PropertyChanged}"></TextBox>
>        <Label Content="{Binding Path=LabelText}"></Label>
>    </StackPanel>
></Window>
where ViewModel (I bodged the MaxLength) is:
    public class ViewModel : DependencyObject, INotifyPropertyChanged
>    {
>        int MaxLength = 183;
>
>        public string LabelText
>        {
>            get
>            {
>                if ( TheText==null || TheText.Length == 0)
>                    return string.Format("Max Characters: {0}", MaxLength);
>                else
>                    return string.Format("Remaining Characters: {0}", MaxLength - TheText.Length);
>            }
>        }
>
>
>        private string theText;
>        public string TheText
>        {
>            get { return theText; }
>            set
>            {
>                theText = value;
>                OnPropertyChanged("TheText");
>                OnPropertyChanged("LabelText");
>            }
>        }
>
>        public event PropertyChangedEventHandler PropertyChanged;
>
>        void OnPropertyChanged(string s)
>        {
>            if (PropertyChanged != null)
>                PropertyChanged(this, new PropertyChangedEventArgs(s));
>        }
>    }
You could, of course bind the label directly to the TextBox.Text.Length in XAML and use a ValueConverter but I assumed you wanted a clean MVVM approach....

I actually haven't implemented it yet as I am bogged down in more important functional items. We deploy to QA tonight so in a bit of a hurry. I plan to come back to this as a "Nice to have" feature while in this QA cycle.
Thanks, I have looked at this and is more inline with what I was thinking.
Thanks
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform