1

このサイトのさまざまな例を試して、リスト ボックス/チェック ボックスのコンボをデフォルトのグレーから別の色に変更して無駄にしました。

私が最終的にやろうとしているのは、アイテムがチェックされている場合、背景は白になり、チェックされていない場合は灰色になります.

ここに私が持っているものがあります。

リソースを以下のコメントに更新します。

コントロールは返信に更新されましたが、まだ機能していません。何かアイデアはありますか?

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            ItemsSource="{Binding}" 
            Name="lstSwimLane" SelectionMode="Multiple"
            Width="auto" 
            Height="auto"
            Background="Transparent"
            BorderThickness="0" 
            SelectionChanged="LstSwimLaneSelectionChanged">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="Border" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBrush}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource UnselectedBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemTemplate>                                    
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
                                Checked="ChkFilterChecked" 
                                Unchecked="ChkFilterUnchecked" 
                                VerticalAlignment="Center" 
                                Margin="0,0,4,0" />
                <TextBlock Text="{Binding Value}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

注: チェックされたチェックボックスとリスト項目の組み合わせは灰色のままで、チェックされていないものは白です。

以下の返信に一致すると思われるスクリーンショットを添付します。私は困惑しています。

スクリーンショット

画像を大きく表示するための直接リンクは次のとおりです。
http://s1120.photobucket.com/albums/l489/nitefrog/?action=view¤t=jw_0012011-03-311325.jpg

チェックボックスのスクリーンショットを次に示します。

ここに画像の説明を入力

http://i1120.photobucket.com/albums/l489/nitefrog/jw_0022011-03-311345.jpg

ブラシが設定されているにもかかわらず、何らかの理由でそれらがトリガーされていません。

ここに画像の説明を入力

何か案は?

ありがとう。

4

1 に答える 1

3

私の例ではスタイルを使用していませんmyListboxStyle。削除できます。ただし、ItemContainerStyleプロパティを変更します。

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border x:Name="Border" SnapsToDevicePixels="true">
                                <ContentPresenter />
                            </Border>

                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBrush}"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="Border" Property="Background" Value="{StaticResource UnselectedBrush}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

これは非常に単純なテンプレートで、トリガーはIsSelected=Trueとの 2 つだけIsSelected=Falseです。この例を完了するには、次のブラシをResourcesコレクションに追加します。

    <SolidColorBrush x:Key="SelectedBrush" Color="White"/>
    <SolidColorBrush x:Key="UnselectedBrush" Color="Gray"/>

の標準スタイルを編集した方がよいのですがListViewItem、インターネットで見つけることができず、現在 Expression Blend を持っていません。

結果の画面: ここに画像の説明を入力

于 2011-03-31T10:05:24.557 に答える