我在列表框中動態創建一個堆棧面板集合。在這個堆棧面板中包含水平對齊的標籤和複選框。由於顏色導致列表框中的選定項無法讀取
問題是,當我點擊一個堆疊面板時,選擇是不可讀的,因爲該行變成深藍色,而字母保持黑色,如此黑的藍色,你什麼都看不到。 如何動態更改堆疊面板中選定元素的前景色?我動態地說,而不是在xml文件中,因爲所有這些元素都是從數據庫動態創建的。
我有類似下面的代碼:
foreach (var utilis in item.user)
{
StackPanelWithID ligne = new StackPanelWithID();
ligne.Orientation = Orientation.Horizontal;
ligne.ID = utilis.TRIGRAMME;
ligne.Height = 21;
Label l = new Label();
l.Width = 120;
Label l2 = new Label();
l2.Width = 145;
CheckBox cbEntretien = new CheckBox();
}
contentpresenter將無法正常工作......我嘗試了幾種方法來定位它... 於是,我找到了一種方法來圈問題... 在App.xaml中:
<Application.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</Style.Resources>
</Style>
</Application.Resources>
因此所選項目的背景是清晰的,這樣用戶仍然可以讀取所選listboxitems的文本。
和每個listboxitem有關。
但是...我很想知道如何在列表框中更改所選項目的文本顏色..如果我設法得到答案,我會讓你保持聯繫...
我這樣做...
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background"
Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ContentPresenter>
</ControlTemplate>
但stll不工作,它說的觸發屬性不能在ControlTemplate中找到...... 我試圖把它的觸發屬性之後添加,但不工作...
我試圖在App.xaml中是這樣的: 「
<Style x:Key="SimpleListBoxItem" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" <!--can't find the text property so try to act on the Background color to set it to a different color than dark blue-->
Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>"
,並在特定的XAML文件在我的列表框是:
<ListBox Margin="9,64,8,313" Loaded="lstUtilisateurs_Loaded" Name="lstUtilisateurs" ItemContainerStyle="{StaticResource SimpleListBoxItem}"/>
但在執行的時候,出現了什麼在列表框中,什麼也沒有...我不明白...
你的對象是動態創建的,但風格可以靜態設置。例如,您可以在WPF中使用控件模板。 – xandy 2010-03-24 14:53:32
當你說你動態創建列表框的堆疊面板時,你的意思是你以編程方式創建它們嗎? – 2010-03-24 16:42:59
是的,我的意思是編程對不起 我有什麼是類似的東西: 「的foreach(VAR刺在item.user) {StackPanelWithID LIGNE =新StackPanelWithID(); ligne.Orientation = Orientation.Horizontal; ligne.ID = utilis.TRIGRAMME; ligne.Height = 21; 標籤L =新標籤(); l.Width = 120; 標籤L2 =新標籤(); l2.Width = 145; 複選框cbEntretien =新的複選框(); ... 我要去嘗試模板,但它對我來說很難...... – Anna 2010-03-24 17:31:26