wpf WPF如何使用StringConverter将滑块的值绑定到标签的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17604140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:12:13  来源:igfitidea点击:

WPF how to bind slider's value to label's content with StringConverter?

c#.netwpfvisual-studio

提问by iAteABug_And_iLiked_it

I have a simple sliderand a plain label. Label's content is bound to slider's value and it works fine- when you move the slider , label's content changes e.g 23.3983928394 , 50.234234234 and so on

我有一个简单的滑块和一个普通的label。标签的内容绑定到滑块的值并且它工作正常 - 当您移动滑块时,标签的内容会发生变化,例如 23.3983928394 、 50.234234234 等等

I'd like to round it to int values. 1,2,10....100. But when I try to use a converter I get the "ivalueconverter does not support converting from a string".

我想将其四舍五入为 int 值。1,2,10....100。但是当我尝试使用转换器时,我得到 “ivalueconverter 不支持从字符串转换”。

How can I convert the slider's Value to an int in the converter?

如何在转换器中将滑块的值转换为 int?

Thank you

谢谢

This is my XAML

这是我的 XAML

<Grid.Resources>
        <local:MyConvertor x:Key="stringconverter" />
    </Grid.Resources>
<Slider x:Name="mySlider" Height="50" Width="276" Maximum="100"/>
<Label Content="{Binding ElementName=mySlider, Path=Value, Converter=stringconverter}" />

This is my stringconverterclass

这是我的stringconverter

public class MyConvertor: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //How to convert string to int?
    }

采纳答案by LPL

To answer the question: the error is thrown because of

回答问题:抛出错误是因为

Converter=stringconverter

it has to be

它一定要是

Converter={StaticResource stringconverter}

In your converter you convert not string to int but double(Slider.Value) to object(Label.Content) which can be a string too. e.g.

在您的转换器中,您不是将 string 转换为 int,而是将double( Slider.Value) 转换为 object( Label.Content),它也可以是字符串。例如

return ((double)value).ToString("0");

or

或者

return Math.Round((double)value);

回答by keyboardP

You could just use the StringFormatproperty here instead of creating a converter. The double value will be rounded to an int due to the #format specified.

您可以在此处使用StringFormat属性而不是创建转换器。由于#指定的格式,double 值将四舍五入为 int 。

<TextBlock Text="{Binding ElementName=mySlider, Path=Value, StringFormat={}{0:#}}"/>

If you want to keep the label, instead of using a TextBlock, you can use ContentStringFormatinstead as Contenttakes in an objecttype.

如果你想保留的,而不是使用一个TextBlock标签,你可以使用ContentStringFormat而不是作为Content发生在一个object类型。

<Label Content="{Binding ElementName=mySlider, Path=Value}" ContentStringFormat="{}{0:#}" />

回答by Erwin Draconis

you can use the Label's ContentStringFormat property

您可以使用标签的 ContentStringFormat 属性

<Label Content="{Binding ElementName=Slider, Path=Value}" 
   ContentStringFormat="{}{0:N0}" />

回答by ChuckMorris42

Check the Slider's IsSnapToTickEnabled property

检查 Slider 的 IsSnapToTickEnabled 属性