如何设置ListBox项的前景色

时间:2011-02-10 05:59:44

标签: windows-phone-7 colors listbox

我在WP7应用程序页面中有一个ListBox,它绑定到名为Location的自定义对象的集合(List)。在那个对象是一个名为WMO的字段,当ListBox加载时我想要做的是设置任何绑定列表框项目的foregound颜色具有与我的默认值相同的值...但我似乎无法让这个工作我读过或谷歌搜索没有任何帮助。

我知道列表中的项目绑定到数据源但我想要获得该项目的物理表示并更改前景颜色....只是无法理解我是如何做到的,所以如果有人可以帮助我很感激。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" >
    <ScrollViewer Height="615"  HorizontalAlignment="Left" Margin="5,5,5,5" Name="scrollViewer1" VerticalAlignment="Top">
        <ListBox Name="lbxSavedLocs" Height="615" FontSize="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" SelectionChanged="lbxSavedLocs_SelectionChanged" Loaded="lbxSavedLocs_Loaded">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="380" Text="{Binding SiteName}" HorizontalAlignment="Left" />
                        <TextBlock Width="90" Text="{Binding WMO}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>
</Grid>
private void lbxSavedLocs_Loaded(object sender, RoutedEventArgs e)
{
    //Populate the listbox from our saved locations.
    lbxSavedLocs.ItemsSource = gl.savedLocs.OrderBy(x => x.SiteName);

    foreach (Location itm in lbxSavedLocs.Items)
    {
        if (loc.WMO == gl.defaultWMO)
        {
          //GET AN "INVALID CAST" EXCEPTION HERE:
          ((ListBoxItem)itm).Foreground  = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
        }
    }

    //Hopefully this produces a redraw of the ListBox.
    lbxSavedLocs.InvalidateArrange();
}

3 个答案:

答案 0 :(得分:6)

试试这个:

选项1:

 ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));
lbi1.Foreground= new SolidColorBrush(Color.FromArgb(100, 45, 23, 45));

选项2:

ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items.SelectedItem));

 lbi2.Foreground= new SolidColorBrush(Colors.Red);

答案 1 :(得分:1)

如果您需要将相同的前景颜色应用于ListBox中的所有项目,或者将前景颜色绑定到数据项中的值,则最佳方法是修改ItemContainerStyleItemContainerStyle定义围绕ItemTemplate内容的可视化包装器,默认情况下使用ContentControl来设置或绑定Foreground属性:

        <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="LayoutRoot" 
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Background="{TemplateBinding Background}"
                                HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalAlignment}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" 
                                                                           Storyboard.TargetName="LayoutRoot">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimation Duration="0" 
                                                             To=".5"
                                                             Storyboard.TargetProperty="Opacity" 
                                                             Storyboard.TargetName="ContentContainer"/>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionStates">
                                    <VisualState x:Name="Unselected"/>
                                    <VisualState x:Name="Selected"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentControl x:Name="ContentContainer"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Content="{TemplateBinding Content}"
                                            Foreground="#FFFF0000" 
                                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                            Margin="{TemplateBinding Padding}"
                                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Background="Black"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

答案 2 :(得分:0)

这是一种方式......我认为它有效

要绑定的属性为Foreground,所以

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0" >
    <ScrollViewer Height="615" HorizontalAlignment="Left" Margin="5,5,5,5" Name="scrollViewer1" VerticalAlignment="Top">
        <ListBox Name="lbxSavedLocs" Height="615" FontSize="22" HorizontalAlignment="Left" VerticalAlignment="Top" Width="470" SelectionChanged="lbxSavedLocs_SelectionChanged" Loaded="lbxSavedLocs_Loaded">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="380" Text="{Binding SiteName}" Foreground="{binding forgroundColor}" HorizontalAlignment="Left" />
                        <TextBlock Width="90" Text="{Binding WMO}" Foreground="{binding forgroundColor}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>
</Grid>

在程序中设置了apt条件,在其中填充列表框以使用foregroundColor

选择正确的Forground

使用class containsin

填充列表框
class lisboxItem
{
    public string SiteName{get;set;}
    public string forgroundColor{get;set;}
    public string WMO{get;set;}
}

创建List<listboxItem> items=new List<listboxItem>();

并在一个循环中填写列表items,其中u给出了你想要的条件。

之后 呼叫 lbxSavedLocs.ItemSource=items