-1

I'm trying to optimize the XAML code for providing accurate binding mode for all the binding in XAML, but could not find the appropriate one, according to me i think binding mode should be 'OneWayToSource', Suggest proper binding mode.

<RadButton x:Name="btnClose" Command="{Binding CloseCommand, Mode=?}"/>
james tudu
  • 9
  • 2
  • 2
  • None at all, the default OneWay if sufficient. "Optimize" you XAML by setting Binding Modes only when necessary. – Clemens Jun 12 '19 at 12:53
  • @jjTudu [According to the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.bindingmode?view=netframework-4.8), `OneWayToSource` would copy the command from `RadButton` to the viewmodel. If you have information to the contrary, I urge you to contact Microsoft. – 15ee8f99-57ff-4f92-890c-b56153 Jun 12 '19 at 15:45
  • "premature optimization is the root of all evil" – Kevin Cook Jun 12 '19 at 18:11
  • @KevinCook premature optimization means? we already have Application in prod. Seniors have this requirement! – james tudu Jun 13 '19 at 06:25
  • @Clemens So when should it be necessary? – james tudu Jun 13 '19 at 06:27
  • 1
    Only when the default value is not sufficient, so hardly ever. For most properties, the default value covers the standard use cases. It's e.g. TwoWay by default for the Text property of a TextBox. And of course OneWay for all those properties that are never set by the target object, like the Command property of a Button. In an idealistic WPF application, you might not find a single Binding that explicitly sets its Mode. – Clemens Jun 13 '19 at 07:46
  • 2
    So a requirement that demands the explicit specification of the Mode of all Bindings in a WPF application is plain nonsense. – Clemens Jun 13 '19 at 07:51
  • @jjTudu How did these “seniors” measure the overhead caused by bindings updating, and how did they determine that it’s the most important inefficiency to be addressed in the application? I’d bet money it’s not in the top twenty, and I’d bet more money they measured nothing. Keep your head down and collect your paycheck. – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '19 at 11:37

3 Answers3

0

Assuming that the CloseCommand property value is static, after being set in the ViewModel's constructor, then Mode=OneTime will be marginally more efficient as the binding won't include any checks for future updates.

However, as others have pointed out, this requirement of specifying a mode for every single binding is completely facile. The default binding mode, as specified in the DependencyProperty definition, should cover 99% of cases.

Peregrine
  • 4,287
  • 3
  • 17
  • 34
0

If your command never changes, the right mode is Mode=OneTime. You won't notice a performance improvement for a few buttons but when the binding is on a element of a big list, it is always a good idea to apply the appropriate mode. With OneTime, WPF does not need to monitor the binding in case there is a change in the source and/or the target.

There is a good description of the various mode here.

Maxence
  • 12,868
  • 5
  • 57
  • 69
-2

In your view model,

public DelegateCommand CloseCommand { get; }

In the constructor

CloseCommand = new DelegateCommand(closeCommand);

then add the function

private void closeCommand()
{
    //Do Something
}