質問

アイテムのリストを表示し、どちらが「現在」であるかを示したいシナリオがあります。アイテム(小さな矢印マーカーまたは変更された背景色)。

" SelectedItem"のコンテキストが必要なため、

ItemsControlは役に立ちません。ただし、選択をプログラムで移動し、ユーザーが変更を許可しないようにします。

ListBoxを非インタラクティブにする簡単な方法はありますか?意図的にマウスとキーボードのイベントを飲み込むことでそれをかじることができますが、私たちが望むものを提供するいくつかの基本的なプロパティが欠けていますか?

または...両方の世界で最高の別のWPFコントロール、SelectedItemプロパティを持つItemsControlがありますか?

役に立ちましたか?

解決

1つのオプションは、 ListBoxItem.IsEnabled false に設定することです:

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

他のヒント

同じ問題がありました。 IsEnabledをtrueに設定したまま、ListBoxのPreviewMouseDownイベントを処理することで解決しました。編集したくない場合は、ハンドラーでe.Handledをtrueに設定します。

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

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>

添付プロパティを使用した選択不可のListBoxItem(またはListViewItem)機能: http:// thrash505。 wordpress.com/2011/01/04/non-selectable-listboxitem-or-listviewitem-using-attached-properties/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top