c# - 带参数的 slider 的 ThumbToolTipValueConverter

标签 c# xaml win-universal-app

<Slider ThumbToolTipValueConverter="{StaticResource ThumbConverter}"/>

我需要根据 Slider.Value 和 ViewModel 中的值(称为 SecondValue)更改 ThumbToolTip 中的值。

如何将 SecondValue 传递给 ThumbConverter? (我如何在这里使用ConverterParameter?)

最佳答案

类似的事情:

转换器 -

public class ThumbConverter : DependencyObject, IValueConverter
{
    public double SecondValue
    {
        get { return (double)GetValue(SecondValueProperty); }
        set { SetValue(SecondValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SecondValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SecondValueProperty =
        DependencyProperty.Register("SecondValue", typeof(double), typeof(ThumbConverter), new PropertyMetadata(0d));


    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // assuming you want to display precentages

        return $"Precentage: {double.Parse(value.ToString()) / SecondValue}";
    }

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

用法 -

<Slider VerticalAlignment="Top">

        <Slider.ThumbToolTipValueConverter>

            <converters:ThumbConverter SecondValue="{Binding SecondValue}" />

        </Slider.ThumbToolTipValueConverter>

    </Slider>

请注意 - 仅当 slider 的值发生变化时才会发生视觉变化。 虽然类本身会收到有关 SecondValue 更改的通知,但仅当您更改 slider 的值时才会发生视觉变化。

情况迫切需要实现 MultiValueConverter,但我们在 UWP 中没有。所以这是我拥有的最干净的一个。

关于c# - 带参数的 slider 的 ThumbToolTipValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565956/

相关文章:

c# - 什么是命名管道 (net.pipe) 限制?

c# - 导出到 Excel,并更改工作表名称

wpf - 从代码隐藏设置 ContentControls 内容属性会引发异常 'value does not fall in range'

wpf - 是否可以在后面的代码中声明命名空间

windows-8.1 - 在通用应用程序共享项目中包含 .targets 文件以包含自定义构建操作

c# - 在 Universal App 的表格中显示数据

c# - 当外键为空时,使用 Lambda 表达式在 List<T> 中排序

c# - 修剪 url 字符串。 C#

silverlight - 如何在 XAML 中使用 Segoe UI 符号

c# - 如何在 Windows 身份验证中传递默认凭据