WP8 - 绑定一个浮点值到字符串转换 [英] WP8 - Binding a value with a float to String conversion

查看:21
本文介绍了WP8 - 绑定一个浮点值到字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有几个仪表,但我不知道如何将转换器添加到文本绑定.我在 msdn 上阅读了一些指南,但我没有弄清楚(我已经为 WP8 编码了几个星期).

I have a couple of gauges in my app, and I can't figure out how to add a converter to the text binding. I read a couple of guides on msdn but I didn't manage to figure that out (I've been coding for WP8 for just a couple of weeks).

这是一块仪表:

<gauges:MarkerGaugeIndicator Value="0" 
                                             gauges:LinearGaugeRange.IndicatorOffset="35"
                                             x:Name="GaugeBarValore"
                                             IsAnimated="True">
                                    <gauges:MarkerGaugeIndicator.MarkerTemplate>
                                        <DataTemplate>
                                            <Grid Width="73" Height="35" UseLayoutRounding="False" d:LayoutRounding="Auto" Margin="10,-2,0,0">
                                                <TextBlock x:Name="GaugeBarPercent" Text="{Binding}"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           FontSize="20"
                                           FontWeight="Thin" Margin="6,4,32,4" Width="35"/>
                                                <Grid.RenderTransform>
                                                    <CompositeTransform Rotation="90" TranslateX="49" TranslateY="12" />
                                                </Grid.RenderTransform>
                                            </Grid>
                                        </DataTemplate>
                                    </gauges:MarkerGaugeIndicator.MarkerTemplate>
                                </gauges:MarkerGaugeIndicator>

绑定本身有效,但是当值从一个舍入值移动到另一个值时,我可以看到很多十进制数.我想添加这样的转换器:

The binding itself works, but I can see a lot of decimal numbers while the value moves from a round value to another. I want to add a converter like this method:

private String double2String(double valore)
    {
        return Convert.ToString(Math.Round(valore)) + "%";
    }

我只是不知道把这个方法放在哪里,以及如何将它作为一个转换器添加到绑定中.

I just don't know where to put this method and how to add this as a converter inside the binding.

感谢您的帮助!:)塞尔吉奥

Thank you for your help! :) Sergio

推荐答案

创建一个类来保存实现 IValueConverter 接口的 Converter 方法,示例类如下.您必须实现方法 Convert 和 ConvertBack.

Create a class to hold your Converter method that implements IValueConverter interface, Example class is bellow. You have to implement method Convert and ConvertBack.

public class DoubleToString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Math.Round((double)value).ToString() + "%";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return double.Parse(value as string);
    }
}

然后将命名空间添加到您的 XAML 页面.

then add the namespace to your XAML page.

xmlns:convert="clr-namespace:Your_project_name"

接下来将您的转换器作为资源类型 i 添加到您的 XAML 页面..

Next add your converter as a Resource type i to your XAML page..

<phone:PhoneApplicationPage.Resources>
    <convert:DoubleToString x:Key="DoubleConvert" />
</phone:PhoneApplicationPage.Resources>

x:Key 值是我们将在绑定语句中调用的名称.

The x:Key value is the name we are going to call in our binding statement.

然后执行数据绑定.我有一个简单的滑块和一个文本块,滑块值绑定到文本块文本属性

Then perform the data binding. I have a simple slider and a textblock with sliders value bound to the textblocks Text property

<StackPanel>
        <Slider Name="slider" Maximum="100" Minimum="0"  />
        <TextBlock Text="{Binding Value, ElementName=slider, Converter={StaticResource DoubleConvert}}" />
</StackPanel>

这篇关于WP8 - 绑定一个浮点值到字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆