0

我在 FlipView 中有一个 ListView

    <FlipView 
        x:Name="flipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        TabIndex="1"
        Width="Auto"
        Grid.Row="2"
        Grid.Column="1"
        VerticalAlignment="Top"
        HorizontalAlignment="Center"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}" Padding="0" VirtualizingStackPanel.VirtualizationMode="Standard">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <!--
                    UserControl chosen as the templated item because it supports visual state management
                    Loaded/unloaded events explicitly subscribe to view state updates from the page
                -->
                <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="0,100,0,0">
                            <ListView x:Name="ListofOptions" Height="400" Width="280" 
                                      ItemsSource="{Binding QuestionOptions}" SelectedValue="{Binding Answer,Mode=TwoWay}"
                                      IsEnabled="{Binding IsEnabled,Mode=TwoWay}" >
                                <ListView.ItemTemplate>
                                    <DataTemplate>

                                        <StackPanel Orientation="Horizontal" >
                                            <StackPanel.Resources>
                                                <common:AltBackgroundConverter x:Key="BGConvertor" />
                                            </StackPanel.Resources>
                                            <StackPanel.Background>
                                                <SolidColorBrush  Color="{Binding IndexWithinParentCollection, Mode=OneWay, Converter={StaticResource BGConvertor}}"></SolidColorBrush>
                                            </StackPanel.Background>
                                            <TextBlock Text="{Binding OptionValue}"  />
                                      </StackPanel>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </StackPanel>
                    </Grid>
                </UserControl>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

我编写了一个 ListView 的值转换器来更改替代行的背景。这是Conventor的代码

  public class AltBackgroundConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is int)) return null;
        int index = (int)value;

        if (index % 2 == 0)
            return Colors.White;
        else
            return Colors.LightGray;
    }

    // No need to implement converting back on a one-way binding
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

当列表框不在 FlipView 中时,一切正常,但是当 ListView 在 FlipView 中时,Conventor 不会执行。请给我建议。

4

1 回答 1

0

在 VS2012 中创建了一个新的拆分 XAML 项目,并在其中添加了您的转换器并使用它,在ListView移动. 我现在猜测这是一个绑定问题,因为根绑定对象发生了变化,并且其中一个绑定没有像我们预期的那样解决。您是否尝试将标签移动到上层?ListViewFlipViewResourcesFlipeView

PS这更多是评论,但我没有评论的声誉!

于 2013-08-11T10:04:41.867 回答