Search code examples
c#wpfxamlobservablecollection

How can I add a new entry to my ObservableCollection


I'm using an ObservableCollection to generate a List in my WPF app.

public partial class NameList : ObservableCollection<SetCredentialsForAD>
{
    public NameList() : base()
    {
        using var forest = Forest.GetCurrentForest();
        Forest currentForest = Forest.GetCurrentForest();
        DomainCollection domains = currentForest.Domains;
        foreach (Domain objDomain in domains)
        {
            Add(new SetCredentialsForAD("domain", "name", "password"));
        }
    }
}

public class SetCredentialsForAD
{
    private string loginName;
    private string passWord;
    private string domainName;

    public SetCredentialsForAD(string domain, string lName, string pWord)
    {
        this.domainName = domain;
        this.loginName = lName;
        this.passWord = pWord;
    }

    public string DomaineName
    {
        get { return domainName; }
        set { domainName = value; }
    }

    public string LoginName
    {
        get { return loginName; }
        set { loginName = value; }
    }

    public string PassWord
    {
        get { return passWord; }
        set { passWord = value; }
    }
}

Xaml:

<ListBox x:Name="CredentialList" Width="auto" Height="auto"
    Grid.Column="0" Grid.Row="2" Style="{StaticResource CredentialList}" 
    SelectionMode="Multiple" 
    ItemsSource="{Binding Source={StaticResource NameListData}}"  
    ItemTemplate="{StaticResource NameItemTemplate}"                                                  
    IsSynchronizedWithCurrentItem="True"/>

xaml datatemplate:

<c:NameList x:Key="NameListData"/>

    <DataTemplate x:Key="NameItemTemplate">
            <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
                <TextBlock x:Name="DomainNameForCredentials" FontSize="18"  Grid.Row="2" Grid.Column="0" Text="{Binding Path=DomaineName, Mode=TwoWay}" Style="{StaticResource CredentialListTextBlock}" ></TextBlock>
                <Label Grid.Row="1" Grid.Column="1" Content="samAccountName" Style="{StaticResource CredentialListLabel}" ></Label>
                <Label Grid.Row="1" Grid.Column="2" Content="Passwort" Style="{StaticResource CredentialListLabel}"></Label>
                <TextBox x:Name="samAccountNameForCredentials" Grid.Row="2" Grid.Column="1" Text="{Binding Path=LoginName, Mode=TwoWay}" Style="{StaticResource CredentialListTextBox}" />
                <TextBox x:Name="passwordForCredentials" Grid.Row="2" Grid.Column="2" Text="{Binding Path=PassWord, Mode=TwoWay}" Style="{StaticResource CredentialListTextBox}"/>
        </Grid>
    </DataTemplate>

and I need to add a new Item on Click to the List to display it in my UI and do other stuff with it.

I've tried some things to add en empty listItem like:

var setCredentialsforAD = new NameList();
setCredentialsforAD.Add(new SetCredentialsForAD("","",""));

What is the right way to solve my problem?

thank you


Solution

  • I would look into how to use an MVVM pattern in your application. I think the approach you are taking makes things more difficult. How to do that is beyond the scope of an answer. I only can answer this question because I am familiar with your other post.

    How to get Data from WPF Data Template with c#

    The point of an ObservableCollection is that it implements INotifyPropertyChanged on Adds/Removes. Which means when you Add an item the UI is notified of that change and will update accordingly. You do not want to reinstantiate ObservableCollections.

    Based on the information in the previous post and what is above to accomplish what you need would be

    private void SaveDomainCredentials_OnClick(object sender, RoutedEventArgs e)
    {
        if ( CredentialList.ItemsSource is NameList nameList )
        {
            nameList.Add(new SetCredentialsForAD("", "", ""));
        }
    }