WPF:仪表值转换器 [英] WPF: gauge value converter

查看:30
本文介绍了WPF:仪表值转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有简单的 Gauge 类和静态 int 属性,实现了 Propertychanged:

So i have simple Gauge class with static int property that implement Propertychanged:

TotalPacketsSent

这个 property 一直在增加,我想写一个简单的 converter 并发送这个 converter 这个 property> value 并根据这个 property 返回一些值:

This property is raising all the time and i want to wrote simple converter and send this converter this property value and return some value base on this property:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
int val = (int)value;
    double percentage = ((double)MyClass.TotalPacketsSent / MyClass.TotalPacketsInList) * 100;
    return percentage;
        }

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

Window.Resources 下我有这个:

<Convertors:GaugeValueConverter x:Key="GaugeValueConverter"/>

这是我的Gause:

<Controllers:Gauge x:Name="gauge"
                   Value="{Binding Path=(my:MyClass.TotalPacketsSent), Converter={StaticResource GaugeValueConverter}}"
                   Minimum="0"
                   Maximum="100"/>

所以我的问题是我的 converter 根本不工作,我的意思是我看不到它甚至执行了.任何想法为什么?

So my issue is that my converter not working at all, i mean that i cannot see that this even executed. Any ideas why ?

这个 property 一直在变化,我有 Label 我用这种方式来显示它的价值:

This property is changing all the time and i have Label i am using this way to show its value:

Content="{Binding Path=(my:MyClass.TotalPacketsSent)}"

这很好用.

推荐答案

您将 value 参数转换为 int 参数,然后不使用它.

You're casting the value argument as an int and then not using it.

我想你正在寻找的是类似的东西(派生自 IMultiValueConverter 而不是 IValueConverter):

I imagine what you're looking for is something like (deriving from IMultiValueConverter rather than IValueConverter):

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    double totalPacketsSent = (double)values[0];
    double totalPacketsInList = (double)values[1];

    // further validation for handling divide by zero, etc. may need to go here

    return totalPacketsSent / totalPacketsInList * 100
}

在 XAML 中:

<Controllers:Gauge x:Name="gauge" Minimum="0" Maximum="100">
    <Controllers:Gauge.Value>
        <MultiBinding Converter="{StaticResource GaugeValueConverter}">
            <Binding Path="(my:MyClass.TotalPacketsSent)" />
            <Binding Path="(my:MyClass.TotalPacketsInList)" />
        </MultiBinding>
    </Controllers:Gauge.Value>
</Controllers:Gauge>

这篇关于WPF:仪表值转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆