ValueConverter not invoked in DataTemplate binding

2019-07-21 18:49发布

问题:

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}}"/>