none
Button doesn't update if it is outside of a page or user control (MVVM applied)

    Question

  • I am trying to disable a button when a textbox has no value in it and enable it when there is value.

    Structure of the project:

    XAML window:

    - contains the button that needs to be disabled

    - contains the user control 

    - DataContext - ViewModel.cs

    User control:

    - contains the textbox

    - contains a test button to check the behavior (will be removed once everything goes working)

    DataContext - ViewModel.cs

    INotifyPropertyChanged is enabled and RaisePropertyChanged functions updates the interface with properties' values.

    Both buttons are bound the same way to the view model:

    <Button Content="{Binding ButtonText,Mode=OneWay}" Command="{Binding NextButton}"  Grid.Row="1"/>

    Result:

    pasting outside image link as I am not able to post images (account not verified):

    https://photos.app.goo.gl/j2GUTR1qwNMoxHDp6

    So as you can see the bottom button doesn't update as it is outside the user control. The button which is part of the user control updates perfectly.

    How to solve that?

    Sunday, May 19, 2019 5:37 AM

All replies

  • Your button in Window is not working because it don't "see" the UserControl I think.

    One of the posible fix is 

    1. add name to your User Control

    <UserControl x:Class ...., x:Name="UCName", ... />

    2. then add it in Binding inside Window button.

    <Button Content="{Binding ButtonText, ElementName=UCName, Mode=OneWay}" Command="{Binding NextButton, ElementName=UCName}"  Grid.Row="1"/>

    It should working, I have no experience with INotify I use only DependencyProperties which is working for me all times.


    Sunday, May 19, 2019 10:44 AM
  • Hi BlagovestPeshev,

    According to your description, there are one usercontrol and Button in Mainwindow, there are one TextBox and One Button in Usercontrol window, now, you want to enbale these two button when TextBox have some text, and disable these Button when TextBox is empty, am I right?

    If yes, I do one sample that you can take a look:

    UserControl:

     <StackPanel>
            <TextBox
                Name="textbox1"
                Width="200"
                Height="30"
                Margin="20" TextChanged="Textbox1_TextChanged"
                Text="{Binding dynamictext, RelativeSource={RelativeSource AncestorType=local:UserControl12}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <Button
                Name="btn1"
                Width="200"
                Height="30"
                Content="btn1"
                IsEnabled="False" />


     public partial class UserControl12 : UserControl
        {
            public string dynamictext
            {
                get { return (string)GetValue(dynamictextProperty); }
                set {
                  
                    SetValue(dynamictextProperty, value);
                   
                }
            }
    
            // Using a DependencyProperty as the backing store for dynamictext.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty dynamictextProperty =
                DependencyProperty.Register("dynamictext", typeof(string), typeof(UserControl12), new PropertyMetadata(null));
    
                 
    
            public UserControl12()
            {
                InitializeComponent();
            }
    
            private void Textbox1_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (string.IsNullOrEmpty(dynamictext))
                {
                    btn1.IsEnabled = false;
                }
                else
                {
                    btn1.IsEnabled = true;
                }
            }
        }

    MainWindow:

     <StackPanel>
            <usercontrol:UserControl12 x:Name="usercontrol1" dynamictext="{Binding value1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <Button
                Name="btn2"
                Width="200"
                Height="30"
                Margin="20"
                Content="btn2"
                IsEnabled="False" />
        </StackPanel>
     public partial class Window27 : Window, INotifyPropertyChanged
        {
            private string _value1;
            public string value1
            {
                get { return _value1; }
                set
                {
                    _value1 = value;
                    if(!string.IsNullOrEmpty(value1))
                    {
                        btn2.IsEnabled = true;
                    }
                    else
                    {
                        btn2.IsEnabled = false;
                    }
                    RaisePropertyChanged("value1");
    
                }
            }
            public Window27()
            {
                InitializeComponent();
                this.DataContext = this;
            }
            
            public event PropertyChangedEventHandler PropertyChanged;
    
             
            public void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 3:14 AM
    Moderator