Programing

WPF의 목록 상자 항목을 선택할 수 없도록 설정

crosscheck 2020. 12. 28. 21:49
반응형

WPF의 목록 상자 항목을 선택할 수 없도록 설정


WPF에 목록 상자가 있는데 항목을 선택하면보기 흉한 색상이 표시됩니다. 모든 항목을 선택 불가능하게 만들 수 있습니까?


당신이 선택을 필요로하지 않는 경우,을 사용 ItemsControl보다는ListBox


ListBoxItem 스타일에서 Focusable 속성을 false로 추가합니다.

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

선택 가능한 것을 원하지 않는다면 아마도 목록보기를 원하지 않을 것입니다. 그러나 이것이 정말로 필요한 것이라면 스타일로 할 수 있습니다.

<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>

IsSelected 트리거를 살펴보십시오. 테두리를 다른 색으로 만들어 "추악"하지 않게 만들거나 투명하게 설정하여 선택했을 때 보이지 않게 할 수 있습니다.

도움이 되었기를 바랍니다.


목록 상자에서 이것을 사용하십시오. 이 매우 우아한 해결책을 찾았습니다.

<ListBox ItemsSource="{Binding YourCollection}">
    <ListBox.ItemContainerStyle>
       <Style TargetType="{x:Type ListBoxItem}">
           <Setter Property="Focusable" Value="False"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

더 쉬운 방법이 있습니다 : set ListBoxproperty IsHitTestVisible="False". 이렇게하면 목록의 모든 항목이 마우스 이벤트를받지 못합니다. 이렇게하면 마우스를 가져갈 때 강조 표시가 중지되는 이점도 있습니다.

WP 7.1에서 나를 위해 작동합니다.


이를 수행하는 간단한 방법 (위의 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;
    }
}

이벤트를 피하는 것이 더 좋으며 더 우아하고 부작용이없는 Style 태그입니다.

<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>

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


누군가가 여전히 선택할 수없는 ListBoxItem (또는 ListViewItem) 기능을 원하는 경우. http://thrash505.wordpress.com/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/


You can also make disabled Listbox, which will give you static, non-interactive listbox.

<ListBox IsEnabled="False"/>

I think this is the solution as simple as possible.


In my case I had templated ListboxItems with a Textblock and a ComboBox. The only "active" should be the Combo...

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

did work for me as expected. BR, Daniel

ReferenceURL : https://stackoverflow.com/questions/1722456/make-listbox-items-in-wpf-not-selectable

반응형