ListBox
カスタム ユーザー コントロールを含む基本を持つWPFがある場合、で選択されたときに表示状態を変更するItemTemplate
ようにユーザー コントロールに指示するにはどうすればよいですか?DataTemplate
ListBox
あなたが与えることができるどんな助けにも感謝します
ListBox
カスタム ユーザー コントロールを含む基本を持つWPFがある場合、で選択されたときに表示状態を変更するItemTemplate
ようにユーザー コントロールに指示するにはどうすればよいですか?DataTemplate
ListBox
あなたが与えることができるどんな助けにも感謝します
ListBoxItem
プロパティでトリガーするようにスタイルを設定できIsSelected
ます。次に例を示します。
そして、次のように使用します。
<ListBox ItemContainerStyle="{StaticResource yourListBoxItemStyle}">
ListBox
またはそれ自体で直接:
<ListBox.ItemContainerStyle>
<Style TargetType=”ListBoxItem”>
...
</Style>
</ListBox.ItemContainerStyle>
編集:
半不透明なレイヤーを選択した項目の上に配置するItemTemplate
と の両方を使用した完全な例を次に示します。ItemContainerStyle
<Grid>
<Grid.Resources>
<x:Array Type="sys:String" x:Key="sampleData">
<sys:String>Red</sys:String>
<sys:String>Green</sys:String>
<sys:String>Blue</sys:String>
</x:Array>
<DataTemplate x:Key="listBoxItem">
<Rectangle Fill="{Binding}" Width="100" Height="100"/>
</DataTemplate>
<Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<ContentPresenter />
<Rectangle x:Name="Rectangle" Fill="Black" Opacity="0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Rectangle" Property="Opacity"
Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<ListBox
ItemsSource="{StaticResource sampleData}"
ItemTemplate="{StaticResource listBoxItem}"
ItemContainerStyle="{StaticResource listBoxItemStyle}"
/>
</Grid>
編集
コメントの後、「統合された」テンプレートを使用したバージョンを次に示します。
<Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Rectangle Name="Rectangle" Fill="{Binding}" Width="100" Height="100" Opacity="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Rectangle" Property="Opacity"
Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>