Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This website has greatly benefited me, but now I have to ask a question of my own.

I am new to C# and MVVM applications.. Now I am building an app with different views, one view is the navigation view which should show other views depending on the navigation buttons. These buttons depend on the entered value in a view.

Example: There is a navigationView containing a ContentControl and two buttons (nextStep and previousStep). I have put several textboxes in a view (parameterView) and associated model (parameterViewModel) which is displayed in the ContentControl. When all parameters are entered, the user may, by means of a button (nextStep mentioned before), go to the next step/view (checkDataView). Now the button (in navigationView) must therefore be visible when all parameters are filled in parameterView, and hidden when one parameter is not filled in. The nextStep button should activate another page in the ContentControl.

I can navigate with checkboxes or radio buttons, but only without dependence on values ??in another viewModel.

What should I do to get the dependency of parameters in another viewModel?

My NavigationView ContentControl and buttons are defined as:

        <ContentControl Grid.Row="1"
                        Grid.ColumnSpan="5"
                        Content="{Binding CurrentItemView}" />

        <Button Grid.Column="0"
                Grid.Row="2"
                Content="Next Step"
                Style="{StaticResource SubMenuButton}"
                Visibility="{Binding PreviousStepCommandVisibility}"
                Command="{Binding PreviousStepCommand}"/>

        <Button Grid.Column="3"
                Grid.Row="2"
                Content="Previous Step"
                Style="{StaticResource SubMenuButton}"
                Visibility="{Binding NextStepCommandVisibility}"
                Command="{Binding NextStepCommand}"/>

My ViewModel of above View:

namespace SomeApp.MVVM.ViewModel
{
    class GenerateMenuViewModel : BaseViewModel
    {
        public RelayCommand PreviousStepCommand { get; set; }
        public RelayCommand NextStepCommand { get; set; }

        private Visibility _previousStepCommandVisibility;

        public Visibility PreviousStepCommandVisibility
        {
            get { return _previousStepCommandVisibility; }
            set { _previousStepCommandVisibility = value; }
        }

        private Visibility _nextStepCommandVisibility;

        public Visibility NextStepCommandVisibility
        {
            get { return _nextStepCommandVisibility; }
            set { _nextStepCommandVisibility = value; }
        }

        public SomethingViewModel SomethingVM { get; set; }

        private object _currentItemView;
        public object CurrentItemView
        {
            get { return _currentItemView; }
            set
            {
                _currentItemView = value;
                OnPropertyChanged();
            }
        }

        public GenerateMenuViewModel()
        {
            SomethingVM = new SomethingViewModel();

            CurrentItemView = SomethingVM;
        }
    }
}

TextBoxes in View2, which values give dependence to the navigation buttons, are defined as:

<TextBox Text="{Binding Paramater1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Paramater2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Paramater3, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

The ViewModel which belongs to the above View:

namespace SomeApp.MVVM.ViewModel
{
    class View1ViewModel : BaseViewModel
    {
        public View1ViewModel()
        {

        }

        private string _parameter1;

        public string Parameter1
        {
            get { return _parameter1; }
            set { _parameter1 = value; OnPropertyChanged(); }
        }
        
        private string _parameter2;

        public string Parameter2
        {
            get { return _parameter2; }
            set { _parameter2 = value; OnPropertyChanged(); }
        }

        private string _parameter3;

        public string Parameter3
        {
            get { return _parameter3; }
            set { _parameter3 = value; OnPropertyChanged(); }
        }
       
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

The simple way is to add a property in viewModel to indicate that all the parameters are validated and succeeded.

class View1
    {
        public bool Isvalid { get => !validationResults.Values.Contains(false); }

        Dictionary<string, bool> validationResults = new Dictionary<string, bool>
        {
            { nameof(Val1), false }
        };

        string val1 = "";
        public string Val1
        {
            get => val1; set
            {
                val1 = value;
                OnPropertyChanged();
                validationResults[nameof(Val1)] = !string.IsNullOrEmpty(value); //Validation goes here
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...