2011-02-10 37 views
3

我有一個WP7應用程序頁面中的列表框,我綁定到名爲位置的自定義對象的集合(列表)。在那個對象是一個名爲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(); 
} 

回答

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> 

在忍受,你填入你的列表框右邊Forground使用選擇恰當的條件程序

使用填充類列表框中containin

class lisboxItem 
{ 
    public string SiteName{get;set;} 
    public string forgroundColor{get;set;} 
    public string WMO{get;set;} 
} 

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

,並填寫在一個循環,你給你想要的狀態列表items

是 通話後 lbxSavedLocs.ItemSource=items

+0

謝謝 - 我會試一試,但這是你測試過的東西,還是隻是猜測? – nzmike

+0

我已經測試過了。 – VichitraVij

+0

我的要求是動態改變某個/某些特定列表框項目的背景! – VichitraVij

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); 
+0

感謝的是,你的第一個答案是唯一一個爲我的默認位置是由用戶編程,而不是確定的沒有在的SelectedItem我環,我可以使用選擇一個。但是,當我嘗試下面的代碼時,「lbi1」始終爲空......任何想法爲什麼這可能是? – nzmike

1

如果您需要將相同的前景色適用於所有項目在ListBox或綁定fo將顏色重新調整爲數據項中的值,則最佳方法是修改ItemContainerStyle。該ItemContainerStyle限定圍繞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>
+0

謝謝Derek,但是我想要做的就是根據綁定對象中的值等於其他地方設置的默認值,更改我的代碼中列表框中一行的顏色。我很欣賞塞特斯特是非常強大的(我在別處使用它們),但是爲了我想要做的事情,這看起來有點像用錘子砸碎這個諺語的堅果!絕對欣賞輸入。 :-) – nzmike

+0

正如我在我的回答中提到的,你也可以綁定顏色。所以你的數據對象會暴露顏色,並在大多數情況下返回默認顏色,並根據需要返回自定義顏色。 –

相關問題