0

リストボックスにスタックパネルのコレクションを動的に作成します。このスタックパネルには、水平方向に整列されたラベルとチェックボックスが含まれています。

問題は、スタックパネルをクリックすると、行が濃い青になるのに対し、文字が黒のままで青地に黒く、何も表示されないため、選択が読み取れないことです。スタックパネルで選択した要素の前景色を動的に変更するにはどうすればよいですか? これらの要素はすべてデータベースから動的に作成されるため、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}"/>

しかし、実行すると、リストボックスには何も表示されなくなり、何も表示されなくなります...わかりません...

4

1 に答える 1

2

それでも問題があるかどうかはわかりませんが(最後の回答は2010年3月25日でした)、これを達成する方法をまだ疑問に思っている人のために、私は次のようにしました:

スタイル部分:

    <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">

        <Style.Resources>

            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> <!-- makes the background color transparent, removes backcolor border-->


            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/> <!-- Sets the textcolor of the selected text to red -->

        </Style.Resources>
    </Style>

Listbox では、次のような ItemContainerStyle プロパティを使用します。

ItemContainerStyle="{StaticResource myLBStyle}

探すのに時間がかかりましたが、ここにあります。どなたか使っていただけると幸いです!

また便利:

http://msdn.microsoft.com/en-us/library/ms603164.aspx

よろしくお願いします、

サム

于 2011-02-13T11:01:31.337 に答える