简体   繁体   中英

why does the converter not receive a value while binding

I have an observable collection of objects bound to a tabcontrol from which I am generating tabs by binding it as an itemssource

    <TabControl x:Name="tabSubmodule" ItemsSource="{Binding}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TabStripPlacement="Left">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <!--
                <TextBlock Text="{Binding ., Converter={StaticResource NameConverter}}"/>
                <Label Content="{Binding ., Converter={StaticResource NameConverter}}"/>
                -->
                <ContentPresenter Content="{Binding ., Converter={StaticResource NameConverter}}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding ., Converter={StaticResource NameConverter}}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

I am supossed to trigger the Converter in the itemtemplate and based on the object generate the name of the tab and some other stuff. As you can see, the binding occurs (Path) on the whole object. I have a breakpoint in converter's ConvertMethod. It triggers fine.

The problem is, when the breakpoint triggers from the ItemTemplate, the "value" argument of the method is always null. I tried setting it to various controls (because of object or string expectation - content, text propetty). nothing. It is always null

When the converter triggers from the Contenttemplate, the value is not null, but the expected object.

any suggestions?

I generally do a sanity check in my converters, and if the value is null, return Binding.DoNothing . Sometimes your converter will get a null value. The key is to not throw an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM