Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

UserControl Binding not updating the ViewModel

$
0
0

Hello, i am having a difficult time understanding why a property in my view model is not updating.

      ***** I have a user control named FxcmLoginView. This control has a dependency property called ErrorMessage

public string ErrorMessage
        {
            get { return (string)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }        
        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register("ErrorMessage", typeof(string), typeof(FxcmLoginView), new PropertyMetadata(""));

***** Inside the FxcmLoginView User control  the ErrorMessage property is bound to a textblock

<TextBlock Margin="5" Name="errMsgTextBlock" Foreground="White" TextWrapping="Wrap"                            
                           Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=views:FxcmLoginView},
                           Path=ErrorMessage, Mode=TwoWay}">  

**** in my main window i use the FxcmLoginView and i bind it to the ViewModel.

<views:FxcmLoginView 
            AccountID="{Binding Username,Mode=TwoWay}"
            Password="{Binding Password,Mode=TwoWay}"
            AccountType="{Binding AccountType,Mode=TwoWay}"
            LoginCommand="{Binding LoginCommand}"
            IsProgressBarVisible="{Binding IsAccountConnecting}"
            ErrorMessage="{Binding ErrorMessage,Mode=TwoWay}">            
        </views:FxcmLoginView>

the property ErrorMessage inside the ViewModel has all the necessary property change requierements. So inside the view model when i change the ErrorMessage property it reflects correctly inside the user control. The issue is that there is a button inside the FxcmLoginView control that when it is clicked it sets the ErrorMessage dependendcy property of the FxcmLoginView control to an empty string "". After this button is clicked the text changes fine in the TextBlock but my ViewModel never pick up on this change and if in the viewModel i later try to set the ErrorMessage property after the button has been clicked the FxcmLoginView user control wont pick up on the change. I am not sure if this is correct but my assumption is that that button click within the control is messing up the bindings somehow. Below is the xaml of the button inside the FxcmLoginview user control that clears the error Message.

EventTrigger RoutedEvent="mui:ModernButton.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <StringAnimationUsingKeyFrames 
                                        Duration="0"
                                        Storyboard.TargetName="control"
                                        Storyboard.TargetProperty="ErrorMessage">
                                        <DiscreteStringKeyFrame KeyTime="0:0:0.0" Value=""/>
                                    </StringAnimationUsingKeyFrames>
                                   
                                                         
                                </Storyboard>                                
                            </BeginStoryboard>                            
                        </EventTrigger>
                    </mui:ModernButton.Triggers>

Does anyone see something wrong here? i have been debugging for a while and cant seem to figure out why after clicking the button all bindings seem not to work. For example ... in the view model i set the ErrorMessage to a value, lets say ErrorMessage ="Some Message" . and some message is displayed in my user control. i click a button inside the user control that clears the "Some Message" and it does clear it. However my view models ErrorMessage property never picks up this change. If then i set the ErrorMessage property on the ViewModel to "Some Message" again the user control wont display it even though it did before :S

Thanks in advance! to all responses :)


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>