How to enable scrolling when listView.IsEnabled=false

Asked 2 years ago, Updated 2 years ago, 119 views

Creating WPF with C# (.net5) in VisualStudio 2019.

Check boxes and text are placed in the listView in the screen, and the input is made possible.
Among them, we control the activation/inactivity of the entire listView by pressing certain buttons as follows:

listView.IsEnabled=false;

However, if you do this, you won't be able to scroll.
Is it not possible to specify the properties that only scrolls can be used for viewing data?(As far as I checked, I couldn't find it.)

If you have any tricks, please let me know.

c# wpf

2022-09-30 19:39

1 Answers

Enable or disable ListViewItem.
You can scroll, but you won't be able to select or edit items.

Enable or disable control on the cell control.
You can scroll and select items, but only edit them.

Here is an example implementation:

<DockPanel>
    <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Center" Orientation="Horizontal">
        <CheckBox Content="IsItemEnabled" Margin="10"IsChecked="{BindingIsItemEnabled}"/>
        <CheckBox Content="IsCellEnabled" Margin="10"IsChecked="{BindingIsCellEnabled}"/>
    </ StackPanel>

    <ListView ItemsSource="{Binding Items}"Margin="10">

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSourceAnceptorType=ListView}, Path=DataContext.IsItemEnabled}"/gt;
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View>
            <GridView>
                <GridViewColumn Header="No."DisplayMemberBinding="{Binding Number}"Width="64"/>
                <GridViewColumn Header="Check">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked}"IsEnabled="{Binding RelativeSource={RelativeSourceAnceptorType=ListView}, Path=DataContext.IsCellEnabled}"/gt;
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Name}" Width="256" IsEnabled="{Binding RelativeSource={RelativeSourceAnceptorType=ListView}, Path=DataContext.IsCellEnabled}"/gt;
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>

    </ListView>
</DockPanel>
public partial class MainWindow:Window
{
    public MainWindow()
    {
        InitializeComponent();
        This.DataContext=newMainWindowViewModel();
    }
}


public class MainWindowViewModel:INotifyPropertyChanged
{
    # region INotifyPropertyChanged Support

    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty <T> (ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName=null)
    {
        if (Equals(storage, value)) return false;
        storage=value;
        RaisePropertyChanged (propertyName);
        return true;
    }

    protected void RaisePropertyChanged ([System.Runtime.CompilerServices.CallerMemberName] string name=null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

    #endregion

    public List <Person>Items {get;} = Enumerable.Range(0,1000).Select(e=>new Person(e)) .ToList();

    private bool_isItemEnabled = true;
    public bool IsItemEnabled
    {
        get {return_isItemEnabled;}
        set {SetProperty(ref_isItemEnabled, value);}
    }

    private bool_isCellEnabled = true;
    public bool IsCellEnabled
    {
        get {return_isCellEnabled;}
        set {SetProperty(ref_isCellEnabled, value);}
    }
}


public class person
{
    public Person (intn)
    {
        This.Number=n;
        This.IsChecked=n%10==0;
        This.Name=$"Name {n:0000}";
    }

    public int Number {get;set;}
    public bool IsChecked {get;set;}
    public string name {get;set;}
}


2022-09-30 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.