user3761858
user3761858

Reputation: 223

Best practices for WPF - does every GUI element need a binding?

I am struggling to wrap my head around the real benefit of binding in WPF.

I have an application with a large textbox, designed for taking several hundred characters of user input. I have bound this to a "Text" string in my ViewModel. This works OK.

I also have a button with content "Submit". I need to change the content of this button once or twice, so I am doing it in the click event method in the window's code behind. I could, of course, bind the text to the ViewModel, but is it really worth it?

Should everything have a binding? What if I need to display a MessageBox? That will need some logic inside the onclick.

Should click events me as follows:

private void button_Login_Click(object sender, RoutedEventArgs e)
{
   viewModel.DoSomething();
}

..where everything gets handed to the ViewModel?

I know this is a general question but I have tried my best to ask direct, answerable questions.

Upvotes: 2

Views: 1486

Answers (2)

Lee O.
Lee O.

Reputation: 3312

I wouldn't put any code in codebehind. Create an ICommand property in your ViewModel and bind the buttons Command property to it. I use the ICommand implementation from MVVM Light (RelayCommand) but you can create your own or use one of the many other frameworks.

I'd then have a State property (ProcessStatus here) that I use a DataTrigger with to update the text on my button.

ViewModel

public ICommand LoginCommand
{
   get
   {
      return new RelayCommand(() =>
      {
         ProcessStatus = Status.AUTHORIZING;
         DoSomething();
      });
   }
}

private Status _processStatus;
public Status ProcessStatus
{
   get { return _processStatus; }
   set
   {
      if (value ==_processStatus)
         return;
      _processStatus= value;
      RaisePropertyChanged("ProcessStatus");
   }
}

View

<Button Command="{Binding LoginCommand}">
   <Button.Style>
      <Style TargetType="{x:Type Button}">
         <Setter Property="Content"
                 Value="Submit" />
         <Setter Property="IsEnabled"
                 Value="True" />
         <Style.Triggers>
            <DataTrigger Binding="{Binding ProcessStatus}"
                         Value="{x:Static enum:Status.AUTHORIZING}">
               <Setter Property="Content"
                       Value="Authorizing..." />
               <Setter Property="IsEnabled"
                       Value="False" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Button.Style>
</Button>

Upvotes: -1

user1228
user1228

Reputation:

UI concerns are perfectly fine residing in your codebehind.

Business concerns should reside in your ViewModels. The commands and information they expose are what should be bound to elements in your UI.

Since changing the text in a button based on what the button is supposed to do is a UI concern, binding the text of the button to your ViewModel would be pointless.

Upvotes: 2

Related Questions