使用DataTemplate绑定系统数据类型

时间:2012-01-12 13:40:15

标签: wpf binding mvvm

这可能是一个愚蠢的问题,或者我搜索了错误的词:
我有一个ComboBox,想要使用自定义转换器显示双打(显示等效分数) 在DataTemplate中,我必须指定一个DataType,它是Double,但我不知道如何规范它。我确信有一种简单的方法可以做到这一点!

将System-namespace添加到窗口中无法编译

<Window ... xmlns:sys="clr-namespace:System" ...>

在DataType中简单地键入Double或System.Double也不会。

为简单的Double-Type定义ViewModel也不是解决方案,可以吗?!

到目前为止,这是我的代码:

<ComboBox ItemsSource="{Binding Gains}" SelectedItem="{Binding Gain, Mode=TwoWay}">
    <ComboBox.Resources>
        <DataTemplate DataType=" ??????? fract">
            <TextBlock Text="{Binding ., Converter=fractConverter}"/>
        </DataTemplate>
    </ComboBox.Resources>
</ComboBox>

完整解决方案:

使用System命名空间作为 sys:工作!
收益是一个列表&lt; Double&gt;
增益是双重

<Window ... xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>

Combobox的工作原理如下:

<ComboBox ItemsSource="{Binding Gains}" SelectedItem="{Binding Gain, Mode=TwoWay}">
    <ComboBox.Resources>
        <DataTemplate DataType="{x:Type sys:Double}">
            <TextBlock Text="{Binding ., Converter={StaticResource realConverter}}"/>
        </DataTemplate>
    </ComboBox.Resources>
</ComboBox>

2 个答案:

答案 0 :(得分:2)

像这样:

 <DataTemplate DataType="{x:Type sys:Double}">

答案 1 :(得分:0)

您不必指定数据模板 - 它仅用于获取特定于数据类型(隐式)的数据模板:)只需省略您的数据类型即可。

看看这个:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat.aspx

因为它看起来非常接近你正在做的事情。

相关问题