xaml - Xamarin 窗体 : IValueConverter receive null value in Convert method

标签 xaml xamarin binding converter

我有一个 ListView 集,其中项目源作为子项。我想将一个子对象绑定(bind)到一个 View ,该 View 将通过转换器设置颜色。

调用了转换器方法,但我传入的值为 null

除了点,我还使用 Path=/ 但传递给转换器的值仍然是 null。如果我绑定(bind)属性,它很好,但不是当前项。

<ListView x:Name="childListView" 
    VerticalOptions="FillAndExpand" 
    HasUnevenRows="true" 
    ItemSelected="OnItemSelected"
    ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout 
                        BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 
                        Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                        <StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                                <controls:CircleImage>

最佳答案

这是一个有趣的行为。我最近在使用 CarouselView (Forms.Plugin) 时遇到了这个问题,并做了更多研究,结果发现每个 CarouselViewBindingContext由于某种原因多次设置元素。

所以第一次,转换器获得了 null 值,但最终,它被第二次调用并得到了正确的值,所以我改变了我的转换器以优雅地处理 null 值(value)观,它奏效了。

关于xaml - Xamarin 窗体 : IValueConverter receive null value in Convert method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44103919/

相关文章:

ios - 如何在 UILabel 中启用超链接

ios - Xamarin iOS MSBuild - 缺少 OutputPath?

WPF 数据网格 : CanContentScroll property causing odd behavior

c# - 在 System.Windows.Controls.Image 控件中显示 System.Drawing.Bitmap 对象

c# - 如何使用 C# 将动态 XAML 转换为 PNG 或 GIF?

c# - 如何在listpicker windows phone中设置默认选择

c# - 将 Xaml 路径转换为 ​​C# 代码

c# - 在哪里找到 ViewID

java - 如何将 JSF 组件绑定(bind)到支持 bean 属性

wpf - wpf中的字符串格式