使用 IMultiValueConverter 的 Wpf 数据绑定和转换错误

发布于 2024-08-06 08:54:46 字数 1332 浏览 9 评论 0原文

作为学习 WPF 的一部分,我刚刚完成了名为“在 WPF 中使用数据绑定”的 MS 实验室练习 (http://windowsclient.net/downloads/folders/hands-on-labs/entry3729.aspx)。

为了说明 IMultiValueConverter 的使用,提供了一种预编码实现,其中布尔结果用于确定数据绑定是否与当前用户相关。以下是转换操作的代码:

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);
    }

以下是在 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 提供的完成的实验练习也有错误。

有谁知道如何修复它才能让设计师满意?

干杯,
贝里尔

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

To illustrate using an IMultiValueConverter, there is a pre-coded implementation of one where the boolean result is used to determine whether the data binding is relevant for the current user. Here is the code for the convert 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 wiring to use this in the 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>

The code runs fine, but the XAML designer will not load with a "Specified cast not valid." error. I tried a couple of ways to not use a cast, one of which I left uncommented in the code above. The funny thing is a finished lab exercise provided by MS also has the error.

Does anyone know how to fix it to make the designer happy?

Cheers,
Berryl

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-08-13 08:54:46

这里的问题是您使用 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 作为绑定源。

希望这有帮助:)。

Problem here is that you use Application.Current, which is different in Design mode and in runtime.

When you open designer, Application.Current will not be your "App" class (or whatever you name it). Thus there are no CurrentUser property there, and your get that error.

There are multiple ways to fix it. The easiest one is to check if you are in design mode:

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);
}

Another approach would be not using Application.Current as a source for your binding.

Hope this helps :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文