ListBox의 포커스/선택한 항목을 변경하려고합니다. 신청서는 this article을 기준으로합니다. 순간 가 미안 데이터 템플릿을 통해 ListBoxItem의 스타일을 설정하려고하는 것은 :ListBox에서 선택한 항목의 스타일을 변경할 수 없습니다.
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
목록 상자는 다음과 같습니다 :
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding}"
Foreground="Black"
FontFamily="Segoe UI"
FontSize="22"
HorizontalAlignment="Left"
Padding="15,10,0,0"
/>
</DataTemplate>
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}"
Foreground="Red"
FontFamily="Segoe UI"
FontSize="30"
HorizontalAlignment="Left"
Padding="15,10,0,0"
/>
</DataTemplate>
내 생각은 트리거를 사용하는 템플릿 사이를 전환 할 수 있었다
끝에<ListBox x:Name="valuesItemsCtrl"
BorderThickness="0"
ItemContainerStyle="{StaticResource ContainerStyle}"
Background="Transparent"
Tag="{Binding }">
<ListBox.AlternationCount>
<Binding>
<Binding.Path>Values.Count</Binding.Path>
</Binding>
</ListBox.AlternationCount>
<ListBox.ItemsSource>
<Binding>
<Binding.Path>Values</Binding.Path>
</Binding>
</ListBox.ItemsSource>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
나는 다른 목록 상자에 템플릿을 추가 :
<ListBox x:Name="tumblersCtrl"
BorderThickness="0"
Background="Transparent"
ItemsSource="{Binding Tumblers, ElementName=thisCtrl}"
ItemTemplate="{StaticResource TumblerTemplate}">
</ListBox>
도움이나 힌트를 보내 주셔서 감사합니다!
ehm ... 내가 어디에 cs 코드를 넣어야하는지 : S – seveves
대신에 내가 선택한 클래스를 알아낼 수 없다. ViewModel과 ViewModelBase – seveves
@SeveFriede :'ViewModel'은 별도의 코드 파일에있는 별도의 클래스입니다. 'ListBox'에 대한 데이터 컨텍스트를 지정 했습니까? 그렇다면 답변을 업데이트하겠습니다. – Dennis