0

listboxitemが増えるごとに境界線の背景の色を変更したい。

 <ListBox.ItemTemplate>
            <DataTemplate>
                <border x:name: border>
                   <ListBoxItem ItemSource={Binding Example}>
                   </ListBoxItem>
                </border>

何か案は?

4

2 に答える 2

0

「IValueConverter」の TypeConverter は変換をサポートしていません

上記のように境界線を置くと、このエラーが発生します。

ここに私の完全なコードがあります

<ItemsControl x:Name="listaAdd" ItemsSource="{Binding sample}"  Grid.Row="0" Margin="0,221,0,0" Foreground="White" Background="#FF5B5B5B" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch" Grid.Row="1" Width="480" >
                    <Border x:Name="borda" BorderBrush="{Binding Converter=ColorConverter}"  >
                        <ListBoxItem x:Name="listSelected" Foreground="White" IsSelected="True"  VerticalAlignment="Center"  FontSize="{StaticResource PhoneFontSizeLarge}" Content="{Binding Nome}"  Background="{x:Null}" HorizontalContentAlignment="Left" Height="80" DoubleTap="listSelected_DoubleTap">
                        </ListBoxItem>
                    </Border>
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu x:Name="subMenulist">
                            <Button Grid.Column="1" Content="delete"   BorderThickness="0" Margin="0"  Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
                            </Button>
                            <Button Grid.Column="1" Content="compartilhar" Margin="0,0,0,0" x:Name="btnShare" BorderThickness="0"  Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
                            </Button>
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
于 2012-11-21T12:52:19.033 に答える
0

まず、コンバーターの使用方法について、このリンクを確認してください。

次に、XAML で、境界線を次のように記述します。

<Border BorderBrush="{Binding Converter=ColorConverter}">
 ....
<Border>

コンバーターコードを次のように変更します

public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    //Define some random colors
    Color[] colors = { Colors.Blue, Colors.Brown, Colors.Cyan, Colors.Green, Colors.Magenta, Colors.Orange, Colors.Purple, Colors.Yellow, Colors.LightGray };

    return colors[(new Random()).Next(8)];
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{

}
}

したがって、このコードはいずれかの色を動的に返します。また、同じ色が連続して出る可能性もあります。ところで、私は上記のコードをテストしませんでした。

于 2012-11-21T12:23:03.527 に答える