【问题标题】:Alternate ListBox row colors备用列表框行颜色
【发布时间】:2014-04-07 23:44:21
【问题描述】:

我正在尝试用交替的行颜色来修饰这个 ListBox:

<ListBox x:Name="listBox" Background="{x:Null}" SelectionChanged="listBox_SelectionChanged" >
    <ListBox.Resources>
        <local:AltBackgroundConverter x:Name="AltBackgroundConverter" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0" Height="50" Background="{Binding Converter={StaticResource AltBackgroundConverter}}" >
                <TextBlock Text="{Binding Titulo}" Style="{StaticResource ListViewItemTextBlockStyle}"  />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是AltBackgroundConverter.cs

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.Gray;
        }

        // 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();
        }
    }

显然,它不起作用,因为我没有实现如何将行号绑定到转换器。如您所见,我正在绑定整个 ListBox 项。

我认为解决方案应该很简单,但我找不到。

【问题讨论】:

  • 您没有在 Background 属性中绑定属性。尝试将绑定路径设置为您希望值在转换器中的属性。或者考虑这种方法dotnetspark.com/kb/1780-alternate-row-color-listbox-wpf.aspx
  • 这就是问题所在。我不知道要评估哪个属性,因为我找不到绑定行号的方法。此外,WinRT 应用程序不支持触发器。所以,这种方法行不通。 :S

标签: c# xaml listbox windows-runtime windows-phone


【解决方案1】:

您的解决方案目前存在一些问题。

  1. 您与列表项的绑定,而转换器需要一个整数来确定项目是偶数还是奇数。根据您的屏幕截图,您应该将其更改为如下所示:

    Background="{Binding Numero, Converter={StaticResource AltBackgroundConverter}}"

  2. 你的转换器返回一个颜色,而Background 是一个画笔,所以你可以这样修复它:

    public class AltBackgroundConverter : IValueConverter
    {
        private Brush whiteBrush = new SolidColorBrush(Colors.White);
        private Brush grayBrush = new SolidColorBrush(Colors.Gray);
    
        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 whiteBrush;
            else
                return grayBrush;
        }
    
        // 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();
        }
    }
    
  3. 此时应该开始工作,但您的突出显示只会突出显示文本而不是整行。这就是你可以解决这个问题的方法:

    <ListBox.ItemContainerStyle>
        <Style
            TargetType="ListBoxItem">
            <Setter
                Property="HorizontalContentAlignment"
                Value="Stretch"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 2016-08-18
    • 2015-01-27
    • 2011-09-22
    • 2010-09-26
    • 1970-01-01
    • 2021-12-05
    • 2019-01-06
    相关资源
    最近更新 更多