Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void txtUsername_LostFocus(object sender, RoutedEventArgs e)
        {
            string usergrouptxt = txtUsername.Text;
            try
            {
                if (string.IsNullOrEmpty(usergrouptxt))
                {
                    txtUsername.BorderBrush = new SolidColorBrush(Colors.Red);
                }
                else
                {

                }
            }
            catch (SqlException usrgrpexc)
            {
                MessageBox.Show(usrgrpexc.ToString());
            }

        }



How can I do the above operation in my ViewModel in MVVM?
Posted

1 solution

Easily with help of MVVM Light Toolkit. and System.Windows.Interactivity from Blend SDK.
Just add this namespaces:
HTML
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"


and then, in your TextBox XAML inject next lines:
HTML
<textbox x:name="txt1" ....="" xmlns:x="#unknown">
    <i:interaction.triggers xmlns:i="#unknown">
        <i:eventtrigger eventname="LostFocus">
            <cmd:eventtocommand command="{Binding TestCommand,<br mode=" hold=" />                                          Mode=OneWay}" xmlns:cmd="#unknown">
               CommandParameter="{Binding Text,
                                  ElementName=txt1,
                                  Mode=OneWay}"
               MustToggleIsEnabledValue="True" />
        </cmd:eventtocommand></i:eventtrigger>
    </i:interaction.triggers>
</textbox>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900