<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/