我可以将Xamarin Forms ListView与TemplateSelector和ValueConverter一起使用吗?

时间:2020-04-14 18:51:26

标签: xamarin xamarin.forms

我有一个Xamarin ListView,它显示基于不同DataTemplates的项目,效果很好! 我可以在模板定义中按预期使用绑定。 但是,当我尝试在模板中使用ValueConverter时,它永远不会被调用(转换器在我的应用程序的其他位置工作)。

我在这里想念什么?

2 个答案:

答案 0 :(得分:0)

我在模板中用T ListViewemplateSelector测试了有关ValueConverter的演示。

我有两个DataTemplates,用于计算DateOfBirth.Year的值。如果Year超过1980,那么如果年份超过1980,则将listview设置为ValidTemplate,否则,将设置listview。设置为InvalidTemplate

    public class PersonDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate ValidTemplate { get; set; }

        public DataTemplate InvalidTemplate { get; set; }

        protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
        {
            return ((Person)item).DateOfBirth.Year >= 1980 ? ValidTemplate : InvalidTemplate;
        }
    }

在该位置,它将具有0或1的值,然后通过以下代码将其转换为True或False。

    public class MyValueConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value != 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? 1 : 0;
        }
    }

这是我的xaml布局。

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:MyValueConvert x:Key="intToBool" />

            <DataTemplate x:Key="validPersonTemplate">
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.4*" />
                            <ColumnDefinition Width="0.3*" />
                            <ColumnDefinition Width="0.3*" />
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding Name}" TextColor="Green" FontAttributes="Bold" />
                        <Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="Green" />
                        <Label Grid.Column="2" Text="{Binding Location, Converter={StaticResource intToBool}}" TextColor="Green" HorizontalTextAlignment="End" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="invalidPersonTemplate">
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.4*" />
                            <ColumnDefinition Width="0.3*" />
                            <ColumnDefinition Width="0.3*" />
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding Name}" TextColor="Red" FontAttributes="Bold" />
                        <Label Grid.Column="1" Text="{Binding DateOfBirth, StringFormat='{0:d}'}" TextColor="Red" />
                        <Label Grid.Column="2" Text="{Binding Location, Converter={StaticResource intToBool}}" TextColor="Red" HorizontalTextAlignment="End" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:PersonDataTemplateSelector x:Key="personDataTemplateSelector" ValidTemplate="{StaticResource validPersonTemplate}" InvalidTemplate="{StaticResource invalidPersonTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout Margin="20">
        <Label Text="ListView with a DataTemplateSelector" FontAttributes="Bold" HorizontalOptions="Center" />
        <ListView x:Name="listView" Margin="0,20,0,0" ItemTemplate="{StaticResource personDataTemplateSelector}" />
    </StackLayout>

这里正在运行sceenshot。

enter image description here

这是我的演示。

https://github.com/851265601/XFormsData-TempleSelect

答案 1 :(得分:0)

所以最终我能够使事情正常!

这里的问题是我确实在ViewModel中实现了INotifyPropertyChanged,但没有为ViewItems本身实现。 因此,即使我的ViewItem永远不会在ListView-Template中显示的位置改变,但似乎至少有必要对我想与ValueConverter一起使用的属性实施INotifyPropertyChanged。

顺便说一句,使用具有相同ViewItems且没有INotifyPropertyChanged BUT 且没有TemplateSelector的ValueConverter(即作为ListView-HeaderTemplate),一切正常(至少是我期望的)

相关问题