2012-09-12 3 views
0

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> 

도움이나 힌트를 보내 주셔서 감사합니다!

답변

1

및 데이터 트리거 :

<ListBox ItemsSource="{Binding}"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="IsSelected" Value="{Binding IsSelected}"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Name}" 
         Foreground="Black" 
         FontFamily="Segoe UI" 
         FontSize="22" 
         HorizontalAlignment="Left" 
         Padding="15,10,0,0" 
         x:Name="tbName"/> 
      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding IsSelected}" Value="True"> 
        <Setter TargetName="tbName" Property="Foreground" Value="Red"/> 
        <Setter TargetName="tbName" Property="FontSize" Value="30"/> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

경우 바인딩 데이터는 다음 컬렉션으로 구성됩니다.

public class ViewModel : ViewModelBase 
{ 
    public String Name 
    { 
     get { return name; } 
     set 
     { 
      if (name != value) 
      { 
       name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 
    private String name; 

    public Boolean IsSelected 
    { 
     get { return isSelected; } 
     set 
     { 
      if (isSelected != value) 
      { 
       isSelected = value; 
       OnPropertyChanged("IsSelected"); 
      } 
     } 
    } 
    private Boolean isSelected; 
} 

창 코드 숨김은 :

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new[] 
     { 
      new ViewModel { Name = "John", IsSelected = true }, 
      new ViewModel { Name = "Mary" }, 
      new ViewModel { Name = "Pater" }, 
      new ViewModel { Name = "Jane" }, 
      new ViewModel { Name = "James" }, 
     }; 
    } 
} 
+0

ehm ... 내가 어디에 cs 코드를 넣어야하는지 : S – seveves

+0

대신에 내가 선택한 클래스를 알아낼 수 없다. ViewModel과 ViewModelBase – seveves

+0

@SeveFriede :'ViewModel'은 별도의 코드 파일에있는 별도의 클래스입니다. 'ListBox'에 대한 데이터 컨텍스트를 지정 했습니까? 그렇다면 답변을 업데이트하겠습니다. – Dennis

0

템플릿을 변경하려면 DataTemplateSelector을 사용하십시오.

ContainerStyle을 닫고 대신 ListBox.ItemsTemplateSelector을 사용자 정의 datatemplateselector를 정적 리소스로 설정하십시오.

link에서 자세한 예를 찾을 수 있습니다.

편집 : 그냥 트리거에서 이러한 두 가지 속성 설정 DataTemplate을 필요하지 않습니다 귀하의 코멘트에 따르면 사용 ItemTemplate

<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> 
    <Setter Property="Foreground" Value="Black" /> 
    <Setter Property="FontSize" Value="22" /> 
    <Setter Property="FontFamily" Value="Segoe UI" /> 
    <Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="Padding" Value="15,10,0,0" /> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Foreground" Value="Red" /> 
      <Setter Property="FontSize" Value="30" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

내가 원하는 모두는 텍스트 색상 및 폰트 크기를 변경하는 것입니다. 정말 DataTemplateSelector가 필요합니까? 내 문제는 사실 거기에 아무 selectionChanged 이벤트가 없다고 생각 : S – seveves

관련 문제