【问题标题】:ValueConverter not invoked in DataTemplate binding在 DataTemplate 绑定中未调用 ValueConverter
【发布时间】:2010-04-24 02:38:02
【问题描述】:

我有一个使用DataTemplateComboBoxDataTemplate 包含一个绑定,它使用 IValueConverter 将枚举值转换为字符串。问题是从未调用过值转换器。如果我在StatusToTextConverter.Convert() 中设置断点,它永远不会被命中。

这是我的 XAML:

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

我认为这就是隐式绑定到 DataTemplate 所呈现的值的方式。我错了吗?

编辑:对于上下文:我打算在 DataTemplate 旁边显示一个 Image TextBox。如果我无法让TextBox 绑定工作,那么我认为Image 也不会工作。

【问题讨论】:

    标签: wpf data-binding datatemplate


    【解决方案1】:

    在某些情况下,您必须为Binding 显式提供Path。试试这个:

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

    【讨论】:

    • 这行得通(而且我以前从未见过这种语法,非常酷)!但事实证明我实际上并不需要 Path=。在那里。巧合的是,我不小心删除了视图模型中的 StatusChoices 属性。这是我的失败,但至少我从你的回答中学到了一些东西,谢谢!
    • {Binding} 和 {Binding Path=.} 几乎是等价的,但我遇到了几种必须使用其他语法的情况(当然还有一些额外的绑定属性)。
    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2010-11-02
    • 1970-01-01
    • 2011-12-30
    • 2012-07-14
    • 2017-08-26
    相关资源
    最近更新 更多