HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

ListBox with a dynamic number of TextBox elements

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberelementswithlistboxtextboxdynamic

Problem

I am making a kind of form, and I want the user to be able to enter a dynamic number of string inputs. The idea is that a single TextBox will be displayed, and once the user has filled that out, they will be able to add another one, and repeat the process as they keep filling the text boxes. Once there are multiple, they can remove any text box if they no longer want it.

This is accomplished with a ListBox containing a custom DataTemplate which is a DockPanel with a TextBox and two Buttons.

XAML:


    
        
            
                
                    
                        
                    
                
            
        
    
    
        
            
                
                
                
            
        
    


ExcludedPath:

public class ExcludedPath : INotifyPropertyChanged
{
    private Visibility _addButtonVisibility;
    private string _path;

    public Visibility AddButtonVisibility
    {
        get
        {
            return _addButtonVisibility;
        }
        set
        {
            _addButtonVisibility = value;
            NotifyPropertyChanged();
        }
    }

    public string Path
    {
        get
        {
            return _path;
        }
        set
        {
            _path = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(
        [CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(
            this,
            new PropertyChangedEventArgs(propertyName));
    }
}


ViewModel (Relevant Portions):

```
internal ViewModel()
{
SetUpProperties();
}

public ICommand AddExcludedPathCommand =>
new RelayCommand(AddExcludedPath, CanAddExcludedPath);

public ObservableCollection ExcludedPaths { get;
private set; }

public ICommand RemoveExcludedPathCommand =>
new RelayCommand(
RemoveExcludedPath, CanRe

Solution

ExcludedPath

If the value of the properties aren't changed you won't need to raise the PropertyChangedEvent so a simple check like

public Visibility AddButtonVisibility
{
    get
    {
        return _addButtonVisibility;
    }
    set
    {
       if (_addButtonVisibility == value)
       {
           return;
       }
        _addButtonVisibility = value;
        NotifyPropertyChanged();
    }
}


will be better.

ViewModel

private bool CanRemoveExcludedPath(ExcludedPath excludedPath = null)
{
    return ExcludedPaths.Count > 1;
}


You don't use the method parameter, just remove it.

Code Snippets

public Visibility AddButtonVisibility
{
    get
    {
        return _addButtonVisibility;
    }
    set
    {
       if (_addButtonVisibility == value)
       {
           return;
       }
        _addButtonVisibility = value;
        NotifyPropertyChanged();
    }
}
private bool CanRemoveExcludedPath(ExcludedPath excludedPath = null)
{
    return ExcludedPaths.Count > 1;
}

Context

StackExchange Code Review Q#136081, answer score: 4

Revisions (0)

No revisions yet.