ValueConverter not invoked in DataTemplate binding

孤街醉人 提交于 2019-12-11 01:27:07

问题


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.

This is my XAML:

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

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

Edit: 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.


回答1:


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

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


来源:https://stackoverflow.com/questions/2701057/valueconverter-not-invoked-in-datatemplate-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!