DataConverter在DataTemplate绑定中未调用 [英] ValueConverter not invoked in DataTemplate binding

查看:156
本文介绍了DataConverter在DataTemplate绑定中未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox ,它使用 DataTemplate DataTemplate 包含使用 IValueConverter 将枚举值转换为字符串的绑定。问题是值转换器从不被调用。如果我在 StatusToTextConverter.Convert()中放置一个断点,那么它就不会被击中。

I have a ComboBox that uses a DataTemplate. The DataTemplate contains a binding which uses an IValueConverter to convert an enumerated value into a string. The problem is that the value converter is never invoked. If I put a breakpoint in StatusToTextConverter.Convert(), it never is hit.

这是我的XAML:

    <ComboBox ItemsSource="{Binding Path=StatusChoices, Mode=OneWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Converter={StaticResource StatusToTextConverter}}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

我以为这是一个隐含地绑定到值的一个 DataTemplate 正在呈现。我错了吗?

I thought this is how one implicitly binds to the value a DataTemplate is presenting. Am I wrong?

编辑:对于上下文:我打算显示一个图像 DataTemplate 旁边的 TextBox 。如果我无法获得 TextBox 绑定工作,那么我不认为 Image 可以工作,

For context: I intend to display an Image in the DataTemplate alongside that TextBox. If I can't get the TextBox binding to work, then I don't think the Image will work, either.

推荐答案

在某些情况下,您必须明确提供路径 绑定。尝试这样做:

In some circumstances you must explicitly supply a Path for a Binding. Try this instead:

<TextBlock Text="{Binding Path=.,Converter={StaticResource StatusToTextConverter}}"/>

这篇关于DataConverter在DataTemplate绑定中未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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