通过代码将ValueConverter应用于LongListSelector

本文关键字:应用于 LongListSelector ValueConverter 代码 | 更新日期: 2023-09-27 18:05:02

我有PivotPage和一些PivotItems,每个都有自己的LongListSelector:

<phone:PivotItem x:Name="pivotitem1" Header="Headline 1">
... same like in pivotitem2 ...
</phone:PivotItem>
<phone:PivotItem x:Name="pivotitem2" Header="Headline 2">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <phone:LongListSelector x:Name="lst2" Margin="12,0,0,0" ItemTemplate="{StaticResource myLocationsListTemplate}" ItemsSource="{Binding Items}">
        </phone:LongListSelector>
    </Grid>
</phone:PivotItem>
<phone:PivotItem x:Name="pivotitem3" Header="Headline 3">
... same like in pivotitem2 ...
</phone:PivotItem>

所有LongListSelector都使用相同的DataTemplate,所以我全局声明为StaticRessource。

<DataTemplate x:Name="myLocationsListTemplate">
    <Button Click="btn_ShowLocationDetails_Click" Tag="{Binding ID}" Style="{StaticResource mBlankButton}" Margin="1,0,-1,0">
        <StackPanel Margin="0,0,0,15" >
            <Grid VerticalAlignment="Top" Margin="0,0,5,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="120" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" TextTrimming="WordEllipsis" Text="{Binding Name}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Top" Margin="0,0,0,22" />
                <Image Grid.Column="0" Width="138" Height="25" Source="/mAppData/stars-3.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0"/>
                <TextBlock Grid.Column="1" Text="{Binding DistanceInMeterFormatted, FallbackValue=fallback, TargetNullValue=nullvalue, Mode=OneWay}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" Margin="0,0,-3,20" VerticalAlignment="Bottom"/>
                <TextBlock Grid.Column="1" Text="{Binding LastUploadAgo}" TextWrapping="NoWrap" Margin="0" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
            </Grid>
            <Grid VerticalAlignment="Top" Margin="0,10,0,0" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Width="100" Height="100" Source="{Binding PreviewImg1}"/>
                <Image Grid.Column="1" Width="100" Height="100" Source="{Binding PreviewImg2}"/>
                <Image Grid.Column="2" Width="100" Height="100" Source="{Binding PreviewImg3}"/>
                <Image Grid.Column="3" Width="100" Height="100" Source="{Binding PreviewImg4}"/>
            </Grid>
        </StackPanel>
    </Button>
</DataTemplate>
OnNavigateTo中,我给出了一个相应的模型。我通过代码应用模型:
pivotitem1.DataContext = ...
pivotitem2.DataContext = App.ViewSurroundingsData;
pivotitem3.DataContext = ...

App.ViewSourroundingsData是我的ObservableCollection全局存储。

集合包含一个具有许多属性的对象列表。一个特定的属性是一个整数:imgcnt -它保存图像的数量。

现在我想让所有的列表项不可见,当imgcnt为0。

我研究过,我可以使用ValuesConverter。但是我不知道如何在类属性中依赖于我的整数值来编码这个。我不能扩展通过代码更新IValueConverter找到的解决方案。

我不知道如何使整个列表条目不可见,当属性imgcnt为0时。我不知道如何通过代码(在OnNavigateTo)只应用到我的ItemTemplate的pivotitem2转换器。

有人能帮忙吗?

更新:我执行了以下操作,但没有成功(没有显示所有条目):

pivotitem1.DataContext = App.ViewSurroundingsData;
Binding mBinding = new Binding();
mBinding.Source = App.ViewSurroundingsData;
mBinding.Converter = new IntegerToVisibilityConverter();
lst2.SetBinding(LongListSelector.ItemsSourceProperty, mBinding);

public sealed class IntegerToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        var item = (ViewModels.ViewModel_Surroundings)value;
        if (item.imgcnt == 0) 
        {
            return Visibility.Collapsed;
        }
        else 
        {
            return Visibility.Visible;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible;
    }

通过代码将ValueConverter应用于LongListSelector

你不能像这样将imgcnt绑定到按钮的Visibility吗?

    <Button Click="btn_ShowLocationDetails_Click" Tag="{Binding ID}" Style="{StaticResource mBlankButton}" Margin="1,0,-1,0" 
Visibility="{Binding imgcnt, Converter={StaticResource IntegerToVisibilityConverter}}">

然后在你的转换器中:

public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        return (int)value == 0 ? Visibility.Collapsed : Visibility.Visible;
    }

还是我遗漏了什么?: -)