简体   繁体   English

为什么转换器在绑定时没有收到值

[英]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的对象的可观察对象集合,我通过将其绑定为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. 我猜想在itemtemplate中触发Converter,并基于该对象生成选项卡的名称和其他内容。 As you can see, the binding occurs (Path) on the whole object. 如您所见,绑定发生在整个对象上(路径)。 I have a breakpoint in converter's ConvertMethod. 我在转换器的ConvertMethod中有一个断点。 It triggers fine. 它会触发罚款。

The problem is, when the breakpoint triggers from the ItemTemplate, the "value" argument of the method is always null. 问题是,当断点从ItemTemplate触发时,该方法的“值”参数始终为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. 当转换器从Contenttemplate触发时,该值不是null,而是预期的对象。

any suggestions? 有什么建议么?

I generally do a sanity check in my converters, and if the value is null, return Binding.DoNothing . 我通常在转换器中进行完整性检查,如果该值为null,则返回Binding.DoNothing Sometimes your converter will get a null value. 有时,您的转换器将获得空值。 The key is to not throw an exception. 关键是不要抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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