ValueConverter在FlipView中不起作用

时间:2013-08-11 04:18:22

标签: c# microsoft-metro windows-store-apps .net-4.5

我在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来改变替代行的背景。这是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不会执行。请告诉我。

1 个答案:

答案 0 :(得分:0)

在VS2012中创建了一个新的Split XAML项目,并在那里添加了转换器并在ListView中使用它,并且在ListView内移动FlipView之后仍然有效。 我现在猜测这是一个绑定问题,因为根绑定对象已经改变,其中一个绑定没有按我们的预期解决。您是否尝试将Resources标记移至上一级FlipeView

P.S。这更像是评论,但我没有评论的声誉!