2010-03-23 64 views
0

我有一個實際顯示兩列數據的數據綁定列表框。它顯示如下:我怎樣才能使這個顯示列表框?

<UserControl.Resources> 
     <DataTemplate x:Key="AlignedPairs"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="10" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Text="{Binding Fruits}" Grid.Column="0" /> 
       <TextBlock Text="->" TextAlignment="Center" Grid.Column="1" /> 
       <TextBlock Text="{Binding Colors}" Grid.Column="3" /> 
      </Grid> 
     </DataTemplate> 
    </UserControl.Resources> 

      <ListBox Name="lbStuff" Grid.ColumnSpan="2" Grid.Row="1" 
        ItemTemplate="{StaticResource AlignedPairs}"> 
       <ListBox.ItemContainerStyle> 
        <Style TargetType="ListBoxItem"> 
         <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
        </Style> 
       </ListBox.ItemContainerStyle> 
      </ListBox> 

Then Then Itemsource set in codebehind。

但是,基於某種邏輯,我想在其中一列設置一行或一個項目,例如,一個紅色的水果,或大膽的線。我有代碼來確定我想在後面的代碼中區分哪種水果或顏色(通過顏色/粗體),但我無法弄清楚,特別是在自定義列表框顯示的情況下,我如何才能設置特定項目以不同的顏色/粗體顯示。

有沒有人有任何想法?

讓我知道是否需要任何進一步的代碼。乾杯。

編輯:我真正想要做的就是讓問題儘可能簡單......循環遍歷列表框中的每個項目,並檢查一個字符串,如果有匹配項目SOMEHOW在視覺上區分這一點item list/listview中。沒有必要把它做得比它需要的更復雜。爲什麼你無法單獨設置項目前景色超出了我的想象!

編輯2:

它看起來像我幾乎與下面那裏(但它拋出異常,不能正常工作)

  foreach (var li in ListView.Items) 
      { 

       if (someCriteria == true) 
       { 
         ((ListViewItem) li).Foreground = Brushes.Red; 
//exception throw as this is the actual object stored in this location (the Fruit object for example) not the row or listviewitem I want to change the color of so it can't be cast :(
       } 
      } 
+0

你的數據是如何建模的 - 水果是一個字符串,一個水果對象還是一個集合?什麼信息控制顯示,例如你有一個IsPrizeWinningFruit屬性,你想顯示爲粗體,還是你需要基於它的名字,或什麼? – itowlson 2010-03-23 01:20:13

+0

水果和顏色都是對象,例如屬性名稱和代碼。此屏幕顯示兩者之間的關係(水果和顏色)。有三個文本文件fruit.txt,color.txt和relationship.txt。應用程序有三個屏幕 - 水果,顏色和關係。 Frut可以添加刪除水果。顏色添加刪除顏色。關係 - 可以將兩者聯繫起來。試圖在這個屏幕上顯示兩件事情 - 如果一個水果處於關係中但是從fruit.txt中缺失,而相反,如果fruit處於fruit.txt中但不處於relationship.txt中。我已經用另一個列表框顯示第二個例子。 – baron 2010-03-23 02:15:09

回答

1

這就是ViewModel設計模式真正幫助你的地方。

大概你有一個類暴露了屬性FruitColor。創建一個封裝類,公開這兩個屬性,並且還公開FruitBackgroundBrushItemBackgroundBrush。然後你可以綁定到你的模板中的屬性,例如:

<DataTemplate x:Key="AlignedPairs"> 
    <Grid Background={Binding ItemBackgroundBrush}> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="10" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <TextBlock Text="{Binding Fruits}" 
        Background="{Binding FruitBackgroundBrush}" 
        Grid.Column="0" /> 
     <TextBlock Text="->" TextAlignment="Center" Grid.Column="1" /> 
     <TextBlock Text="{Binding Colors}" Grid.Column="3" /> 
    </Grid> 
</DataTemplate> 
+0

啊真棒!仍然完全瞭解數據綁定,這讓事情變得如此簡單。這是一個完美的解決方案,真正幫助我理解數據綁定的力量。非常感謝! – baron 2010-03-24 00:21:34

0

DataTriggers將照顧這。這裏有一個好的底漆:http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers

+0

我可以在該行,顏色或粗體屬性中設置一行或一個項目嗎?這似乎大多數事情,我谷歌搜索說,你只能設置整個列表框的字體,前景,粗體屬性等... – baron 2010-03-23 02:09:43

+0

此外,這個例子不太適合 - >而不是一個X或O,我有一個潛在的名單字符串名稱檢查每個值 – baron 2010-03-23 02:48:20