【问题标题】:ValueConverter doesn't work in FlipViewValueConverter 在 FlipView 中不起作用
【发布时间】:2013-08-12 16:25:47
【问题描述】:

我在 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 不执行。请给我建议。

【问题讨论】:

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


    【解决方案1】:

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

    附:这更像是评论,但我没有cmets的声誉!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多