Windows phone 7 如何设置列表框项目的前景色

Windows phone 7 如何设置列表框项目的前景色,windows-phone-7,colors,listbox,Windows Phone 7,Colors,Listbox,我在WP7应用程序页面中有一个列表框,我将它绑定到名为Location的自定义对象的集合(列表)。在该对象中有一个名为WMO的字段,当ListBox加载时,我要做的是将绑定的ListBox项的前导颜色设置为与默认值相同的值。。。但我似乎无法让它工作,我读过或谷歌搜索过的任何东西都没有帮助 我知道列表中的项目已绑定到数据源,但我希望获得该项目的物理表示并更改前景颜色。。。。只是不知道我是怎么做的,所以如果有人能帮我,我会很感激的 private void lbxSavedLocs_加载(对象发

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

我知道列表中的项目已绑定到数据源,但我希望获得该项目的物理表示并更改前景颜色。。。。只是不知道我是怎么做的,所以如果有人能帮我,我会很感激的


private void lbxSavedLocs_加载(对象发送方,路由目标e)
{
//从保存的位置填充列表框。
lbxSavedLocs.itemsource=gl.savedLocs.OrderBy(x=>x.SiteName);
foreach(lbxSavedLocs.Items中的位置itm)
{
如果(loc.WMO==总账默认WMO)
{
//在此处获取“无效强制转换”异常:
((ListBoxItem)itm.前台=新的SolidColorBrush(Color.FromArgb(255,255,255,255));
}
}
//希望这会重新绘制列表框。
lbxSavedLocs.InvalidateArrange();
}
试试这个:

备选案文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);

如果需要对
列表框中的所有项目应用相同的前景色,或将前景色绑定到数据项中的值,那么最好的方法是修改
ItemContainerStyle
ItemContainerStyle
定义了围绕
ItemTemplate
内容的可视包装器,默认情况下使用
ContentControl
,您可以设置或绑定
前台
属性:

        <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>

这里有一个方法……我认为它是有效的

要绑定的属性是
前台
,因此


在程序中,设置apt条件,在该条件下,您可以填充列表框,使用
foregroundColor

使用containin类填充列表框

类lisboxItem
{
公共字符串SiteName{get;set;}
公共字符串{get;set;}
公共字符串WMO{get;set;}
}
创建一个
List items=new List()

然后将列表中的
填入一个循环中,在该循环中给出所需的条件

之后 呼叫
lbxSavedLocs.ItemSource=items

谢谢,您的第一个答案是我唯一可以使用的答案,因为我的循环中没有SelectedItem,因为默认位置是通过编程确定的,而不是由用户选择的。但是当我尝试下面的代码时,“lbi1”总是空的。。。知道为什么会这样吗?谢谢Derek,但我想做的就是根据绑定对象中的值等于其他地方设置的默认值,在代码中更改列表框中一行的颜色。我很欣赏二传手的强大(我在其他地方也用过),但对于我想做的事情来说,这确实有点像用锤子砸碎谚语中的坚果!尽管如此,还是非常感谢您的意见。:-)正如我在回答中提到的,您也可以对颜色进行数据绑定。因此,在大多数情况下,您的数据对象将显示颜色并返回默认颜色,并根据需要返回自定义颜色。谢谢-我会尝试一下,但这是您测试过的还是只是猜测?我的要求是动态更改某个/某些特定列表框项的背景!不过,我仍在寻找一段代码,它可以帮助我操作单个listboxitem!