WPF - のItemsSourceが結合される場合、リストボックススタイルを無視します
-
20-09-2019 - |
質問
私が作成したWPFでのListBoxをスタイリングます。
私は手動でのListBoxの項目を読み込むときに、スタイリングは完璧に動作します。私の代わりに、静的なリソース(必要な項目を含むのItemsControl)にリストボックスののItemsSourceに結合する場合しかし、スタイリングが完全にドロップされます。
ここでスタイルです。
<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
<Style.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
<ContentPresenter
Grid.Column="1"
Margin="2,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
ここで正しくスタイルを示してListBoxのためのコードです。
<ListBox x:Name="ColumnsList"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1">
<ListBox.Items>
<ListBoxItem>Test</ListBoxItem>
<ListBoxItem>Test2</ListBoxItem>
<ListBoxItem>Test3</ListBoxItem>
</ListBox.Items>
</ListBox>
ここでスタイルを無視ListBoxのためのコードは次のとおりです。
<ListBox x:Name="ColumnsList2"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1"
ItemsSource="{Binding Source={StaticResource Test1}, Path=Items}">
</ListBox>
誰かが助けることができる願って - 私は、このすべてにかなり新たなんだと私は考えることができるすべてのものを試してみましたが、私が読んだすべてはのItemsSourceを設定すると、アイテムを手動で設定するのと同じ結果を持つべきであると信じて私をリード私はこれが働かない理由何らかの理由を見ることができません。
おかげで、
AT
解決
ItemContainerStyle
プロパティを設定するStyle.Resourcesを変更し、それが魅力のように動作するはずです。
<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
<ContentPresenter
Grid.Column="1"
Margin="2,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
あなたがスタイルでStyle
sを定義するときに(SP1前)の古いバージョンでは、これらのスタイルの一つは無視されます。また、あなたが親リソースにスタイルのリソースを設定することができます..
この情報がお役に立てば幸い!
他のヒント
CheckListBoxStyleであなたのTargetTypeがListBoxItemをターゲットされているためですが、あなたはリストボックスのItemSourceプロパティを設定するときには、他の要素(例えばint型)のリストに結合されています。これは、ターゲット・タイプではなく、ListBoxItemのint型なければならないことを意味します。
また、ターゲットの種類を指定しないでください。