WPF - RangeSlider不会只显示整数

时间:2017-05-27 11:47:57

标签: c# wpf rangeslider

我正在开展一个需要定价的项目。所以我使用Extended WPF Toolkit中的RangeSlider来选择价格范围。

我还制作了两个显示较低和较高值的文本块。但问题是它只显示带小数的数字。我想要整数。

<wpfTool:RangeSlider Name="rangeSlider" Grid.Row="4" LowerValue="300" Minimum="300" Maximum="2300" HigherValue="2300" LowerValueChanged="RangeSlider_LowerValueChanged"/>
        <WrapPanel Orientation="Horizontal" Margin="0,0,26.983,0">
            <TextBlock Text="{Binding LowerValue, ElementName=rangeSlider, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBlock Width="50"/>
            <TextBlock Text="{Binding HigherValue, ElementName=rangeSlider, UpdateSourceTrigger=PropertyChanged}" Width="44"/>
            <TextBlock Text="€" Width="11"/>
        </WrapPanel>

2 个答案:

答案 0 :(得分:1)

你只需要一个转换器,就像这样......

internal class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value is double)
        {
            return ((double)value).ToString("f0");
        }
        return "";
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

像这样在XAML中声明......

<Window.Resources>
    <local:MyConverter x:Key="MyConverter"/>
</Window.Resources>

在XAML中像这样引用......

        <TextBlock Text="{Binding HigherValue, Converter={StaticResource MyConverter}, ElementName=rangeSlider, UpdateSourceTrigger=PropertyChanged}" Width="44"/>

答案 1 :(得分:1)

RangeSlider在其模板中有2个Slider控件。您可以使Slider仅选择整数。相关财产为IsSnapToTickEnabled。要更改嵌套滑块的该属性,请在RangeSlider Resources中添加一个样式:

<wpfTool:RangeSlider Name="rangeSlider" Grid.Row="4" 
                     LowerValue="300" Minimum="300" Maximum="2300" HigherValue="2300" 
                     LowerValueChanged="RangeSlider_LowerValueChanged"/>
    <wpfTool:RangeSlider.Resources>
        <Style TargetType="Slider">
            <Setter Property="IsSnapToTickEnabled" Value="True"/>
        </Style>
    </wpfTool:RangeSlider.Resources>
</wpfTool:RangeSlider>