2009-11-12 5 views
48

WPF에서 목록 상자가 있고 항목을 선택하면 추악한 색상이 표시됩니다. 모든 항목을 선택 취소 할 수 있습니까?WPF의 목록 상자 항목을 선택할 수 없도록합니다.

+0

(http://stackoverflow.com/questions/ 1398559/there-is-no-listbox-selectionmode-none-is-there-another-way-to-disable-select) –

+1

농담 해? 이 질문은 2009 년 에서 온 것입니다. 6 년이 지난 지금 당신은 나에게 말하고 있습니까? Ok –

+2

주문하기가 너무 늦습니다. –

답변

84

당신이 선택이 필요하지 않은 경우, 사용 ItemsControl이 아닌 사람이 여전히 비 선택 ListBoxItem의 (또는 ListViewItem이) 기능을 원하는 경우에, ListBox

+15

항상 그렇지는 않습니다. 'ItemsControl'은 가상화를 사용할 때'ScrollIntoView'와 같이'ListBox'가 필요로하는 것들을 할 수 없습니다. –

+3

반드시 그렇지는 않습니다. ListBox의 * original * 선택 메커니즘을 사용하지 않고 기능을 유지하고 싶지 않은 데에는 여러 가지 이유가있을 수 있습니다. 한 가지 예를 들자면 각 이미지의 모서리에 추가 체크 박스를 추가하려는 이미지의 ListBox를 생각해보십시오 선택을 가능하게합니다. 이 체크 박스를 원래의 선택 메커니즘으로 연결해도 ListBox의 원래 클릭 선택을 비활성화 할 수 있습니다. –

12

당신이 그 (것)들을 선택하고 싶지 않는 경우에 당신은 아마 listview를 원하지 않을 것입니다. 그러나 이것은 당신이 정말로 당신이 스타일로 그것을 할 수 있습니다 후 필요한 경우 다음에 isSelected 트리거에서

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Page.Resources> 


<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
     <Border 
      Name="Border" 
      Padding="2" 
      SnapsToDevicePixels="true"> 
      <ContentPresenter /> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsSelected" Value="true"> 
      <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="false"> 
      <Setter Property="Foreground" Value="#888888"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

    </Page.Resources> 
    <Grid> 
    <ListBox> 
     <ListBoxItem>One</ListBoxItem> 
     <ListBoxItem>Two</ListBoxItem> 
     <ListBoxItem>Three</ListBoxItem> 
    </ListBox> 
    </Grid> 
</Page> 

봐. 테두리를 다른 색으로 만들 수 있으므로 "추악한"것이 아니거나 투명하게 설정되어 선택하면 표시되지 않습니다.

희망이 도움이됩니다.

+2

포커스 사각형을 없애기 위해 ** ** 추가하십시오. –

1

ListBox의 SelectionChanged 이벤트를 처리하고 이벤트 처리기에서 선택한 항목을 선택 취소 할 수 있습니다.

3

위와 같은 viky의 대답을 사용하는 간단한 방법은 SelectionChanged()에서 선택한 인덱스를 -1로 설정하는 것입니다.

public void OnListView_SelectionChanged(Object sender, RoutedEventArgs e) 
{ 
    if (null != sender && sender is ListView) 
    { 
     ListView lv = sender as ListView; 
     lv.SelectedIndex = -1; 
    } 
} 
4

더 쉬운 방법이있다는 : ListBox 재산 IsHitTestVisible="False"을 설정합니다. 이렇게하면 목록의 모든 항목에서 마우스 이벤트를 수신하지 못합니다. 이것은 마우스 오버시에도 강조 표시를 멈출 수 있다는 장점이 있습니다.

WP 7.1에서 저에게 적합합니다.

+2

하지만 목록 상자 전체가 응답하지 않습니다. 스크롤바가 포함되어 있습니다. –

+1

@EladKatz : 사실입니다. 그래서 때때로 스크롤을 다시 설정하기 위해'ListBox' 주위에 내 자신의'ScrollViewer'를 추가하게됩니다. –

+0

@DeniseDraper 그건 좋은 생각인데, 내 scrollviewer는 "스크롤 할 수 없다"는 목록이 가득 찼을 때 .. 나는 내용이 스크롤바가 작동하기에 충분하다는 것을 알고 있지만 "비활성"상태로 유지됩니다. – StinkyCat

2

이벤트를 피하기 위해 더 우아하고 스타일 태그가 부작용이 없습니다.

<ListBox> 
    <ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="IsEnabled" Value="False"/> 
    </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     ... what you want as a source ... 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
17

는 ListBoxItem의 스타일에 거짓으로 Focusable이 속성을 추가

<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}"> 
    <!-- Possibly other setters --> 
    <Setter Property="Focusable" Value="False" /> 
</Style> 
+2

이것은 실제 답변입니다. –

0

당신은 또한 당신에게 정적, 비 대화 형 목록 상자를 줄 것이다 해제 목록 상자를 만들 수 있습니다.

<ListBox IsEnabled="False"/> 

가능한 한 간단합니다.

0

제 경우에는 Textbox와 ComboBox를 사용하여 ListboxItem을 템플릿 화했습니다. 유일한 "활성"은 콤보 여야합니다 ...

<ScrollViewer VerticalScrollBarVisibility="Auto" 
       HorizontalScrollBarVisibility="Disabled" 
       CanContentScroll="True" /> 
    <ItemsControl> 
    ....here my content.... 
    </Itemscontrol> 
</ScrollViewer> 

가 예상대로 작동합니다. BR, Daniel

11

목록 상자에서 사용하십시오. 나는이 매우 우아한 해결책을 발견

<ListBox ItemsSource="{Binding YourCollection}"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Focusable" Value="False"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
의 중복 가능성 [목록 상자에서 선택을 비활성화하는 또 다른 방법은 더 ListBox.SelectionMode = "없음"이 없다가?]
+1

실제로 가장 우아한 솔루션 – CCamilo

관련 문제