Resolved ListBox whose items can be edited / discarded /accepted

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
I would appreciate some help in implementing the following with WPF.
I need to have a ListBox displaying a list of strings but some may be empty strings.
Each ListBox item can be edited, discarded, or accepted.
Therefore, besides the editable ListBox I need some other type of WPF control that qualifies the status
of each item (string): "Edit / Discard / Accept" decided by the user.
I thought to use a Radio-Button for each ListBox but this type of control provides only two possible states.
If the ListBox items are editable, then I need only two states' control on each item: Discard / Accept.
Furthermore, all the ListBox items (strings) and their status must be available to the code behind when the user
clicks on a Button "Confirm". Only the strings that have "Accept" status, and are not empty, will be recorded to a database.

Many thanks in advance for your help
 
Why not go the simple route of adding a context menu for each ListBox item? The menu options would be accept, discard, or edit?

If that is too easy, WPF UI is infinitely customizable. You could make the items emulate the columns of a WinForms DataGridView and be able to edit each item in place. I'm not quite sure how you would like accept and discard to act, though.
 
I thought to use a Radio-Button for each ListBox but this type of control provides only two possible states.
I assume you meant checkbox, not a radio button.

Radio buttons are supposed to come in sets. You can have 3 options, but again it is unclear how an "edit" choice would work. If there are only two options, a checkbox would be better.
 
Furthermore, all the ListBox items (strings) and their status must be available to the code behind when the user
If you were using WPF the way it was meant to be used with MVVM, the view model will always have access to the model, and view will faithful reflect the data exposed to it by the view model

If you are using WPF the WinForms style, you can always get back what is being displayed, but have to jump through some hoops.
 
You could also just have a single column WPF DataGrid:

 
Why not go the simple route of adding a context menu for each ListBox item? The menu options would be accept, discard, or edit?

If that is too easy, WPF UI is infinitely customizable. You could make the items emulate the columns of a WinForms DataGridView and be able to edit each item in place. I'm not quite sure how you would like accept and discard to act, though.
Thank you.
I started to think about this next challenge (for me) some time ago. I saw a video showing that a TextBox was placed inside a ListBox.
A TextBox is editable. Then I can add a RadioButton for each ListBox item.
My big problem is the binding. An initial list of strings will be loaded into the ListBox from the code behind. Then the user will have the possibility to edit Accept/Discard just coupling each ListBox item with a CheckBox.
Isn't this a case of Binding TwoWay?
Thank you
 
Yes, that is two way binding.
 
Help, please.

I was to create a new class, as seen in a posted answer, to implement my editable ListBox.
The class is defined as follows:


C#:
public class StringContainer:INotifyPropertyChanged   
{   
private string _Value ; 
public string Value { 
get { 
return _Value ; 
} 
set { 
_Value = value; 
OnPropertyRaised("Value"); 
 
} 
}

I inserted the requested using statement "using System.ComponentModel"
but I cannot add the requested assembly reference "System.ObjectModel.dll"
because it can't be found in the Reference Manager menu.
Maybe because I am targeting .NET Framework 4.8?
Now I am stuck here.
Is there any NuGet package that contains such an assembly?
Otherwise, how can I use InotifyPropertyChanged?
Thank you
 
******** Update. **********

I could install the NuGet package with the required assembly "System.ObjectModel.dll"
Unluckily, now C# compiler tells me that "StringContainer does not implement interface INotifyPropertyChanged".
I thought I had found a good example to get inspiration from but now I am stuck here.
 
nf 4.8 docs INotifyPropertyChanged Interface (System.ComponentModel)
Assembly:System.dll
So you don't need another reference, but you need to implement the interface. Let intellisense help you do that:
1646601068625.png
 
nf 4.8 docs INotifyPropertyChanged Interface (System.ComponentModel)

So you don't need another reference, but you need to implement the interface. Let intellisense help you do that.
Thank you.
Assembly "System.dll" is already among the references of the project.
Anyway, I managed to install the NuGet package that contains "System.ObjectModel.dll".
My class looks as follows:







C#:
public interface INotifyPropertyChanged { }

        public class StringContainer : INotifyPropertyChanged
        {
            private string _value;
            public event PropertyChangedEventHandler PropertyChanged;
            public string Value
            {
                get { return _value; }
                set
                {
                    if (_value == value)                    // CHECK IF VALUE CHANGED
                        return;                                    // DO NOTHING
                    _value = value;                          // CHANGE VALUE
                    OnPropertyChanged("Value");    // PASS CHANGED CONTENT

                }
            }
        }

However, the C# compiler tells me that the name "OnPropertyChanged" does not exist in this context"
I do not know what to do. This code is basically the same as an example posted on
StackOverflow, a par from the variable names.
 
It should be something like the following (YES/NO)?

C#:
public event PropertyChangedEventHandler PropertyChanged; 
private void OnPropertyChanged(string propertyname)
{ 
  if (PropertyChanged != null)
  { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
  } 
}
 
Same as in the example you can do the null check with ?. null-conditional operator
C#:
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
 
Help with CheckBoxes.
My definition of the editable ListBox is as follows:
C#:
<ListBox Grid.Row="0" Grid.Column="0" DataContext="{Binding ElementName=main}" ItemsSource="{Binding NameList}"> 
    <ListBox.ItemTemplate> 
        <DataTemplate> 
            <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"/> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox>

I inquired with the s/w developer who posted the example I am using. I was told that the TexBoxes inside the ListBox will automatically be generated to host all the strings I have.
The number of strings is only known at runtime. That is why I chose the ListBox.
If I place the CheckBoxes outside the ListBox then how can I know how many are needed? There must be a checkBox for each ListBox item. Shall I place the CheckBox inside the ListBox? How?

I am also unsure about where to declare the updating of the ListBox items Two-way.
I am not confident I understand this modality in depth.

Thank you in advance for your help
 
Back
Top Bottom