恐怕你不能改变
StringFormat
不修改
Style
是的。你能做的就是绑定到
Tag
财产
Slider
在你的
风格
:
<TextBlock x:Name="sliderValue"
FontSize="10"
Foreground="Silver"
Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=Slider}}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
…然后处理
ValueChanged
每个人的事件
滑块
并设置
标签
属性设置为
string
以下内容:
private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Slider slider = (Slider)sender;
slider.Tag = slider.Value.ToString("N1");
}
您可能希望将此功能包装为附加行为:
public class SliderFormatBehavior
{
public static string GetStringFormat(Slider treeViewItem)
{
return (string)treeViewItem.GetValue(StringFormatProperty);
}
public static void SetStringFormat(Slider slider, string value)
{
slider.SetValue(StringFormatProperty, value);
}
public static readonly DependencyProperty StringFormatProperty =
DependencyProperty.RegisterAttached(
"StringFormat",
typeof(string),
typeof(SliderFormatBehavior),
new UIPropertyMetadata(null, OnStringFormatChanged));
static void OnStringFormatChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
Slider slider = depObj as Slider;
if (slider != null)
{
if (slider.IsLoaded)
{
SetTag(slider);
}
else
{
slider.Loaded += Slider_Loaded;
}
slider.ValueChanged += Slider_ValueChanged;
}
}
private static void Slider_Loaded(object sender, RoutedEventArgs e)
{
Slider slider = (Slider)sender;
SetTag(slider);
slider.Loaded -= Slider_Loaded;
}
private static void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
SetTag((Slider)sender);
}
private static void SetTag(Slider slider)
{
slider.Tag = slider.Value.ToString(GetStringFormat(slider));
}
}
示例用法:
<Slider ... local:SliderFormatBehavior.StringFormat="N1" />