windows-phone-7 – 如何设置ListBox项的前景色
我在WP7应用程序页面中有一个ListBox,它绑定到名为Location的自定义对象的集合(List).在那个对象是一个名为WMO的字段,当ListBox加载时我想要做的是设置任何绑定列表框项目的foregound颜色具有与我的默认值相同的值…但我似乎无法让这个工作我读过或谷歌搜索没有任何帮助. 我知道列表中的项目绑定到数据源但我想要获得该项目的物理表示并更改前景颜色….只是无法弄清楚我是如何做到的,所以如果有人可以帮助我’我很欣赏它. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0" > <ScrollViewer Height="615" HorizontalAlignment="Left" Margin="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)); } } //Hopefully this produces a redraw of the ListBox. lbxSavedLocs.InvalidateArrange(); } 解决方法这是一种方式……我认为它有效要绑定的属性是Foreground,所以 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0" > <ScrollViewer Height="615" HorizontalAlignment="Left" Margin="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> 在程序中提出了适当的条件,你填充列表框以使用foregroundColor选择正确的Forground 使用类containsin填充列表框 class lisboxItem { public string SiteName{get;set;} public string forgroundColor{get;set;} public string WMO{get;set;} } 创建一个List< listboxItem> items = new List< listboxItem>(); 并在循环中填充列表项,其中u给出您想要的条件. 之后呼叫lbxSavedLocs.ItemSource =项目 (编辑:好传媒网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |