Success Abbey
Success Abbey

Reputation: 21

Find item in listview with gridview and WPF

I am looking for a way to know if my listview contains a value. Below is my code.

public class OnlineUserList
{
    public string Name { get; set; }
    public string Color { get; set; }
}

<ListView x:Name="lvOnlineUsers" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single" SelectionChanged="lvOnlineUsers_SelectionChanged">
            <ListView.View>
                <GridView x:Name="lvOnlineUsersGridView" AllowsColumnReorder="False">
                    <GridViewColumn Header="Online Users" Block.TextAlignment="Center" TextOptions.TextFormattingMode="Display" TextBlock.FontWeight="Bold">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Name="tbOnlineUsersGridView" Text="{Binding Path=Name}" Foreground="{Binding Path=Color}" HorizontalAlignment="Center" VerticalAlignment="Center" TextOptions.TextFormattingMode="Display" Style="{StaticResource ResourceKey=lblLabel}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

public void AddUserToList(string username)
{
   lvOnlineUsers.Items.Add(new OnlineUserList { Name = username, Color = "Black" });
}

Now this is where am having issue

public void RemoveUserFromList(string username)
{
   if(lvOnlineUsers.Items.Contains(username))
       lvOnlineUsers.Items.Remove(username);
}

Upvotes: 0

Views: 769

Answers (2)

mm8
mm8

Reputation: 169270

Until you have looked into MVVM, try this:

for(int i = lvOnlineUsers.Items.Count - 1; i >= 0; --i)
{
    OnlineUserList item = lvOnlineUsers.Items[i] as OnlineUserList;
    if (item != null && item.Name == username)
        lvOnlineUsers.Items.Remove(lvOnlineUsers.Items[i]);
}

Upvotes: 0

You should learn MVVM.

In the mean time, put the items in an ObservableCollection and assign it to the listview's ItemsSource property in your codebehind. Thereafter, repeat after me: Never, ever touch lvOnlineUsers.Items. Never, never, never. Forget that it exists. Everything you do, you interact with the ObservableCollection. Search it, add items to it, remove items from it. The UI will magically and mysteriously update itself.

I'm going to assume this is in MainWindow. If this is in a different view, the constructor will have a different name.

public MainWindow()
{
    InitializeComponent();

    lvOnlineUsers.ItemsSource = _onlineUsers;
}

private ObservableCollection<OnlineUserList> _onlineUsers 
    = new ObservableCollection<OnlineUserList>();

public void AddUserToList(string username)
{
   _onlineUsers.Add(new OnlineUserList { Name = username, Color = "Black" });
}


public void RemoveUserFromList(string username)
{
    //  We don't search _onlineUsers for the string username, because 
    //  _onlineUsers doesn't contain strings. It contains your user class. 
    //  So instead, we look for the user class instance that has the name
    //  we want. 
    var found = _onlineUsers.FirstOrDefault(ou => ou.Name == username);

    if (found != null)
    {
        _onlineUsers.Remove(found);
    }
}

Upvotes: 1

Related Questions