2012-06-29 78 views
1

我有幾個包含數據庫數據的列表。列表框用作圖表的過濾器,並且列表框的外觀應該根據在其他列表框中選擇的內容而改變。在wpf列表框中設置複選框項目的前景文本顏色

這裏是我想要做專一個簡單的例子:

Class Region 
{ 
public int RegionID { get; set; } 
public string RegionName { get; set; } 
} 

Class Country 
{ 
public int CountryID { get; set; } 
public string CountryName { get; set; } 
public int RegionID { get; set; } 
} 

private void fillListBoxes() 
{ 
List<Region> allRegions = getRegions(); 
lstRegionsFilter.ItemsSource = allRegions; 
} 

一個國家顯然屬於一個區域,而且我也有,例如港口,然後將其位於國家等等

所有列表框項目是這樣定義的複選框:

<ListBox Name="lstRegionsFilter"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Content="{Binding Path=RegionName}" 
         Tag="{Binding Path=RegionID}" 
         Click="CheckBox_Click"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

正如在任何物品列表框中單擊將它們添加到過濾器將過濾接列表r顯示圖表的數據。例如,如果在「地區」下選擇「歐洲」,則屬於歐洲的所有國家/地區的國家/地區的列表框中的顏色應該不同,例如藍色。

所以在代碼中,我想通過在國家列表框中複選框環和依靠,如果顯示的值設置其前景顏色的東西/標記該複選框是屬於選定區域的國家如此通常在foreach循環中。但是,列表框中的項目屬於Region類型,所以如何訪問基礎複選框?這應該是我所知道的非常基本的東西,但它使我瘋狂!

回答

1

試試這個:

public class Region : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private bool isChecked; 

    private void OnPropertyChaned(string property) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 

    public int RegionID { get; set; } 
    public string RegionName { get; set; } 

    public bool IsChecked 
    { 
     get { return isChecked; } 
     set 
     { 
      if (isChecked != value) 
      { 
       isChecked = value; 
       OnPropertyChaned("IsChecked"); 
      } 
     } 
    } 
} 

public class Country : INotifyPropertyChanged 
{ 
    private readonly Region parentRegion; 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 

    public Country(Region parent) 
    { 
     parentRegion = parent; 
     parentRegion.PropertyChanged += ParentChanged; 
    } 

    private void ParentChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if(e.PropertyName.Equals("IsChecked")) 
     { 
      OnPropertyChanged("IsParentChecked"); 
     } 
    } 

    public int CountryID { get; set; } 
    public string CountryName { get; set; } 
    public int RegionID { get { return parentRegion.RegionID; }} 
    public bool IsParentChecked 
    { 
     get { return parentRegion.IsChecked; } 
    } 
} 

和XAML:

這是地區:

<ListBox Name="lstRegionsFilter"> 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <CheckBox Content="{Binding Path=RegionName}" 
        Tag="{Binding Path=RegionID}" 
        IsChecked="{Binding Path=IsChecked}" 
        Click="CheckBox_Click"/> 
    </DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

這是國家

<ListBox Name="lstCountriesFilter"> 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <CheckBox ... 
        Foreground={Binding IsParentChecked, Converter={StaticResource boolToBrushConverter}"/> 
        ... 
        /> 
    </DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

注:

  • 您需要實現Converter類(見here
  • 轉換器添加到XAML爲StaticResouce

鄉村複選框前景色會自動改變,如果該區域器isChecked屬性更改(來自ui或代碼隱藏),所以不需要循環。

我還沒有嘗試過,也許你能找到的錯誤,但是我想顯示「哲學史」

希望它可以幫助

+0

嗨Miklós,並感謝您的快速答覆。不幸的是,這不是我正在尋找的。我需要更改列表框中項目的文本顏色,但它們並不是經過選擇/檢查的。顏色需要更改,但不檢查複選框。我試圖在這裏進一步解釋這個案例http://i48.tinypic.com/1zh3czc.png – ardal

+0

其實,這正是你需要的。該約束不是基於該國是否被檢查,而是基於該國的父母區是否被檢查。這就是爲什麼叫做「IsParentChecked」。你應該嘗試實施他的建議。 – Alain

+0

即使在它們上面引發了NotifyPropertyChanged,有時只讀屬性也不會更新。如果試驗更新問題,則聲明爲正常財產。 – GameAlchemist

0

如果我得到你的權利,你的計劃是有國家突出在其地區coulor,如果它的地區被選中? Rq 1:您不必循環checkBox來知道選擇了哪個區域:這是綁定的目的,綁定集合的值本身將受到影響。
Rq2:如果更改代碼中的值,則必須在U.I的區域類中實現INotifyPropertyChanged。要被更新。
如何做到這一點?
1.有一個RegionId - > Boolean的靜態字典。
2.每當RegionId被選中/未選中時,更新該字典並引發一個靜態事件'SelectionDictionnaryUpdated'。
3.現在在Country類中添加一個通知屬性'IsOwnerRegionSelected'。它會在詞典中查看是否選擇了相應的區域。
4.在Country類中添加屬性'CountryRegionColor'。它將返回基於RegionId的顏色,例如RegionId - > Color static dictionnary。
5.在您的DataTemplate中添加一個Trigger,並根據IsOwnerRegionSelected值在DataTrigger中切換顏色,以便在白色和CountryRegionColor之間切換。
6.在國家的構造函數中,將一個處理程序添加到SelectionDictionnaryUpdated中,該處理程序將在「IsOwnerRegionSelected」上的NotifyPropertyChanged。

就是這樣!

  1. 你可以用非共享dictionnary(有RegionId每一個國家 - >您在構造函數中注入布爾dictionnary屬性)使其
  2. 可以讓事情變得更簡單通過具有「CurrentCoulor」:通知屬性,將返回白色或CountryRegionColor,具體取決於是否選擇。只需將您的列表的背景綁定到該屬性,並在您捕獲SelectionDictionnaryUpdated時notifyCurrentCoulor已更改。
+0

MiklósBalogh的解決方案更加優雅,但如果您計劃進行多個地區/國家/地區選擇(expl:選擇2個國家進行足球比賽),您必須考慮像我一樣的解決方案。在這種情況下,你必須去找非靜態的dictionnary版本,每個dic。是國家的選擇者。 – GameAlchemist

相關問題