Silverlight 绑定转换器

发布于 2024-10-30 19:43:45 字数 381 浏览 5 评论 0原文

我正在尝试将一个字段绑定到我的隐蔽器...但这似乎是不可能的...

这就是我正在尝试做的。

  Source="{Binding LeaveApproved,Converter={StaticResource CommentTypeIconConverter},ConverterParameter={Binding TypeOfWorkId}}"

当 TypeOfWorkId 为 5 并且 LeaveApproved 为 null 时,我想要一个不同的图标。所有其他类型的工作都应该有一个空字段,因此那里没有图标。这就是为什么我需要在我的 coverter 中使用 typeOfWorkId 。

有人知道如何做到这一点吗?

格茨

I'm trying to bind a field to my coverter... But it seems like it's not possible...

This is what I'm trying to do.

  Source="{Binding LeaveApproved,Converter={StaticResource CommentTypeIconConverter},ConverterParameter={Binding TypeOfWorkId}}"

I want a different icon for when the TypeOfWorkId is 5 and the LeaveApproved is null. All the other Types of work should have an empty field, so no icon there. That's why I need the typeOfWorkId in my coverter.

Has anybody have any idea how to do this?

Grtz

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

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

发布评论

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

评论(3

幸福%小乖 2024-11-06 19:43:45

ConvertParameter 中无法进行绑定(尽管您可以使用 StaticResource)。
要解决您的情况,您必须创建另一个属性,例如 LeaveApprovedTypeOfWorkId ,它将考虑您的逻辑。

public YourType LeaveApprovedTypeOfWorkId {
  get{
    if(TypeOfWorkId==5 && LeaveApproved == null){
      return //something
    }
    else{
      return //something
    }
  }
}

改为绑定到此属性。
如果LeaveApproved设置了TypeOfWorkId,请不要忘记NotifyPoppertyChange

Binding is not possible in ConvertParameter (though you can use StaticResource).
To solve your case you will have to create another Property say LeaveApprovedTypeOfWorkId which will take into account you logic.

public YourType LeaveApprovedTypeOfWorkId {
  get{
    if(TypeOfWorkId==5 && LeaveApproved == null){
      return //something
    }
    else{
      return //something
    }
  }
}

Bind to this property instead.
Do not forget to NotifyPoppertyChange in case of LeaveApproved or TypeOfWorkId is set

灰色世界里的红玫瑰 2024-11-06 19:43:45

不要设置对象的属性,您将在转换中拥有您的对象。就像这样:

Content="{Binding Converter={StaticResource xxxxxxConverterName }}"

Dont set the property of the object and You will have your object in your convert. Just like that:

Content="{Binding Converter={StaticResource xxxxxxConverterName }}"
箜明 2024-11-06 19:43:45
public class CommentTypeIconConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int typeOfWorkId;
        if (value == null && parameter != null && int.TryParse(parameter.ToString(), out typeOfWorkId) && typeOfWorkId == 5)
            return new BitmapImage(...);
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这对你有用吗?

更好的做法是使用多重绑定。

对于您想要做的事情, -mutiple-bindings-to-a-single-property/" rel="nofollow">http://www.scottlogic.co.uk/blog/colin/2009/06/silverlight-multibindings-how-to-attached-多个绑定到单个属性/

public class CommentTypeIconConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int typeOfWorkId;
        if (value == null && parameter != null && int.TryParse(parameter.ToString(), out typeOfWorkId) && typeOfWorkId == 5)
            return new BitmapImage(...);
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Does this work for you?

It is better practice to use multibinding though for what you want to do..

http://www.scottlogic.co.uk/blog/colin/2009/06/silverlight-multibindings-how-to-attached-mutiple-bindings-to-a-single-property/

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