Programing

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

lottogame 2020. 12. 27. 10:16
반응형

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


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>

There's an even easier way: set ListBox property IsHitTestVisible="False". This prevents all the items in the list from receiving mouse events. This has the advantage of stopping the highlighting as you mouse-over as well.

It works for me in WP 7.1.


A simple way to do this (using the answer from viky above) is to set the selected index to -1 in the SelectionChanged(), as follows.

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

Better to avoid events, it's more elegant and without side effects the Style tag.

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

you can handle SelectionChanged event of ListBox and unselect the selected item in the event handler.


In case someone still wants non-selectable ListBoxItem (or ListViewItem) functionality. 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

반응형