2008-10-02 3 views
13

우리는 항목 목록을 표시하고 "현재"항목 (작은 화살표 표시 자나 배경색이 변경된 항목)을 나타내려는 시나리오가 있습니다.WPF ListBox를 "읽기 전용"으로 설정할 수 있습니까?

ItemsControl은 "SelectedItem"컨텍스트가 필요하기 때문에 우리에게는 좋지 않습니다. 그러나, 우리는 선택 프로그램을 효과적으로 움직이고 사용자가 그것을 변경할 수 없도록하고자합니다.

ListBox를 비 대화식으로 만들 수있는 간단한 방법이 있습니까? 의도적으로 마우스와 키보드 이벤트를 삼켜 서 퍼지 할 수 있지만, 우리가 원하는 것을 제공하는 비주얼 스타일에 영향을주지 않으면 서 "IsEnabled"를 false로 설정하는 것과 같은 기본적인 속성을 놓치고 있습니까?

또는 ... 두 개의 세계에서 가장 좋은 또 다른 WPF 컨트롤 인 SelectedItem 속성이있는 ItemsControl입니까?

답변

17

하나의 옵션은 ListBoxItem.IsEnabledfalse에 설정하는 것입니다 :이 항목이 선택되지 않도록

<ListBox x:Name="_listBox"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="IsEnabled" Value="False"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

,하지만 그들은 당신이 좋아하는 방법 렌더링되지 않을 수 있습니다. 이 문제를 해결하려면 트리거 및/또는 템플릿을 가지고 놀 수 있습니다. 연결된 속성을 사용하여

<ListBox x:Name="_listBox"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="IsEnabled" Value="False"/> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Foreground" Value="Red" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
+0

ControlTemplate이 DataTemplate의 작업을 수행하고 있습니다. IsEnabled에 대한 세터 외에 필요한 것은 입니다.

+0

요엘. 그에 따라 나의 예가 바뀔 것이다. –

1

ItemsControl/ListBox 데이터가 연결되어 있습니까?

각 항목의 배경 브러시를 원본 데이터의 속성에 바인딩하거나 변환기를 통해 속성을 전달할 수 있다고 생각합니다. 같은 뭔가 :

<ItemsControl DataContext="{Binding Source={StaticResource Things}}" ItemsSource="{Binding}" Margin="0"> 
    <ItemsControl.Resources> 
     <local:SelectedConverter x:Key="conv"/> 
    </ItemsControl.Resources> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
     <local:Control Background="{Binding Path=IsSelected, Converter={StaticResource conv}}"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
2

나는 동일한 문제가 있었다. IsEnabled를 true로 설정하고 ListBox의 PreviewMouseDown 이벤트를 처리하여 해결했습니다. 핸들러에서 e.Handled를 true로 설정하면 편집을 원하지 않습니다.

private void lstSMTs_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     e.Handled = !editRights; 
    } 
+0

키보드를 사용하여 선택 사항을 변경할 수 있습니다. 또한 스크롤 막대가 작동하지 않습니다. – Neil

관련 문제