如何在Silverlight中将行样式添加到ListBox ItemTemplate

时间:2018-08-10 11:20:17

标签: c# .net silverlight listboxitem

我在为ListBox.ItemTemplate添加备用行样式时遇到问题

这就是我的编码方式

 <ListBox x:Name="NListBox" ItemsSource="{Binding NList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                     SelectedItem="{Binding SelectedItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                     Style="{StaticResource CommonListBoxStyle}"  Loaded="NListBox_Loaded">
                <ListBox.ItemTemplate >
                    <DataTemplate >
                        <Grid Margin="0" Width="1600" 
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition  Width="6*"/>
                                <ColumnDefinition Width="2*" ></ColumnDefinition>
                                <ColumnDefinition Width="2*" ></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <TextBlock FontSize="20"  Loaded="TextBlock_Loaded"  Grid.ColumnSpan="3" Text="{Binding H}"  />
                            <TextBlock Grid.Row="0" Grid.Column="1" FontSize="12" HorizontalAlignment="Right">Publish Date</TextBlock>
                            <TextBlock Grid.Row="0" Grid.Column="3" FontSize="12"  Text="{Binding PublishDate,StringFormat=\{0:dd.MM.yy\}}" 
                                       />
                            <TextBlock Grid.Row="1" FontSize="15" TextWrapping="Wrap" Grid.ColumnSpan="3" Text="{Binding Description}"  
                                       Height="50" />
                            <TextBlock Grid.Row="2" Grid.Column="0"  FontSize="12" Text="{Binding link}"  />
                            <TextBlock Grid.Row="2" Grid.Column="1" FontSize="12" HorizontalAlignment="Right">Expiry Date</TextBlock>
                            <TextBlock Grid.Row="2" Grid.Column="3"  FontSize="12" Text="{Binding ExpiryDate,StringFormat=\{0:dd.MM.yy\}}" 
                                       />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>

1 个答案:

答案 0 :(得分:2)

这就是我这样做的原因 .xml

<converters:RowStyleConverter x:Key="RowStyleConverter"/>

<ListBox>
<ListBox.ItemTemplate >   
   <DataTemplate >
      <Grid Background="{Binding Converter={StaticResource RowStyleConverter}}"></Grid>
   </DataTemplate >
</ListBox.ItemTemplate >
</ListBox>

C#

  public class RowStyleConverter : IValueConverter
    {
        int counter = 0;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {   
            if (counter % 2 == 1)
            {  
                return new SolidColorBrush(Color.FromArgb(255, 233, 252, 251));                    
            }
            else
            {   
                return new SolidColorBrush(Color.FromArgb(255, 220, 239, 238));
            }               
            counter++;
        }      

    }