リストボックスにスタックパネルのコレクションを動的に作成します。このスタックパネルには、水平方向に整列されたラベルとチェックボックスが含まれています。
問題は、スタックパネルをクリックすると、行が濃い青になるのに対し、文字が黒のままで青地に黒く、何も表示されないため、選択が読み取れないことです。スタックパネルで選択した要素の前景色を動的に変更するにはどうすればよいですか? これらの要素はすべてデータベースから動的に作成されるため、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>
したがって、選択された項目の背景がより明確になり、ユーザーは選択されたリストボックス項目のテキストを引き続き読むことができます。
すべてのリストボックス項目が関係しています。
それでも...リストボックスで選択したアイテムのテキストの色を変更する方法を知りたいです..答えが得られたら、連絡を取り合います...
これは私がしました...
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background"
Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ContentPresenter>
</ControlTemplate>
しかし、まだ機能していません。トリガープロパティが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}"/>
しかし、実行すると、リストボックスには何も表示されなくなり、何も表示されなくなります...わかりません...