我如何绑定一个字符串中的WPF翻番? [英] How can I bind a string to double in WPF?

查看:77
本文介绍了我如何绑定一个字符串中的WPF翻番?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要定一个绑定。问题是,目标是字符串类型,但来源是double类型。
在下面的code VERSIONNUMBER是double类型。当我运行这一点,文本块是空的,没有抛出任何异常。
如何设置这种结合?

 <风格的TargetType ={X:类型MyControl}>
    < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType ={X:类型MyControl}>
                < TextBlock的文本={TemplateBinding属性= VERSIONNUMBER}/>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;


解决方案

您需要一个转换器:

 命名空间Jahedsoft
{
    [ValueConversion(typeof运算(对象),typeof运算(字符串))
    公共类StringConverter:的IValueConverter
    {
        公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
        {
            返回值== NULL?空:value.ToString();
        }        公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
        {
            抛出新NotImplementedException();
        }
    }
}

现在你可以使用这样的:

 < ResourceDictionary中
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:J =CLR的命名空间:Jahedsoft>    <记者:StringConverter X:键=StringConverter/>
< / ResourceDictionary的>...<风格的TargetType ={X:类型MyControl}>
    < setter属性=模板>
        < Setter.Value>
            <的ControlTemplate的TargetType ={X:类型MyControl}>
                < TextBlock的文本={TemplateBinding属性= VERSIONNUMBER,转换器= {StaticResource的StringConverter}}/>
            < /控件模板>
        < /Setter.Value>
    < /二传手>
< /样式和GT;

I want to set a binding. The problem is that the target is of type string but the source is of type double. In the following code VersionNumber is of type double. When I run this, the textblock is empty, without throwing any exceptions. How can I set this binding?

<Style TargetType="{x:Type MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyControl}">
                <TextBlock Text="{TemplateBinding Property=VersionNumber}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>        
</Style>

解决方案

You need a converter:

namespace Jahedsoft
{
    [ValueConversion(typeof(object), typeof(string))]
    public class StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? null : value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Now you can use it like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:j="clr-namespace:Jahedsoft">

    <j:StringConverter x:Key="StringConverter" />
</ResourceDictionary>

...

<Style TargetType="{x:Type MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyControl}">
                <TextBlock Text="{TemplateBinding Property=VersionNumber, Converter={StaticResource StringConverter}}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>        
</Style>

这篇关于我如何绑定一个字符串中的WPF翻番?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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