Wpf data binding using IMultiValueConverter and casting errors

As part of the WPF training, I just finished working with MS Lab Exercise called "Using Data Binding in WPF" ( http://windowsclient.net/downloads/folders/hands-on-labs/entry3729.aspx ).

To illustrate the use of IMultiValueConverter, there is a pre-encoded implementation of the one where the logical result is used to determine whether data binding is relevant for the current user. Here is the code for the conversion operation:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        // var rating = int.Parse(values[0].ToString());
        var rating = (int)(values[0]);
        var date = (DateTime)(values[1]);

        // if the user has a good rating (10+) and has been a member for more than a year, special features are available
        return _hasGoodRating(rating) && _isLongTimeMember(date);
    }

And here is the gasket for use in XAML:

<ComboBox.IsEnabled>
    <MultiBinding Converter="{StaticResource specialFeaturesConverter}">
    <Binding Path="CurrentUser.Rating" Source="{x:Static Application.Current}"/>
    <Binding Path="CurrentUser.MemberSince" Source="{x:Static Application.Current}"/>
    </MultiBinding>
</ComboBox.IsEnabled>

, XAML " ". . , . - , MS, .

- , , ?

Cheers,
Berryl

+3
1

, Application.Current, .

, Application.Current "App" ( , ). , CurrentUser, .

. - , :

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
  if (Application.Current == null ||
      Application.Current.GetType() != typeof(App))
  {
    // We are in design mode, provide some dummy data
    return false;
  }

  var rating = (int)(values[0]);
  var date = (DateTime)(values[1]);

  // if the user has a good rating (10+) and has been a member for more than a year, special features are available
  return _hasGoodRating(rating) && _isLongTimeMember(date);
}

Application.Current .

, :).

+4

Source: https://habr.com/ru/post/1718551/


All Articles