如何将新条目添加到我的ObservableCollection

我正在使用ObservableCollection在我的WPF应用中生成列表。

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数据模板:

<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>

我需要在Click上添加一个新项目到列表,以将其显示在我的UI中并对其进行其他处理。

我已经尝试过一些方法来添加一个空的listItem,例如:

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

解决我的问题的正确方法是什么?

谢谢

kinshen911 回答:如何将新条目添加到我的ObservableCollection

我将研究如何在您的应用程序中使用MVVM模式。我认为您所采用的方法会使事情变得更加困难。如何做到这一点超出了答案的范围。我只能回答这个问题,因为我熟悉您的其他帖子。

How to get Data from WPF Data Template with c#

ObservableCollection的要点是,它在添加/删除时实现INotifyPropertyChanged。这意味着当您添加项目时,UI会收到有关该更改的通知,并将相应地更新。您不想重新实例化ObservableCollections。

根据上一篇文章中的信息以及完成您需要的上述工作

private void SaveDomainCredentials_OnClick(object sender,RoutedEventArgs e)
{
    if ( CredentialList.ItemsSource is NameList nameList )
    {
        nameList.Add(new SetCredentialsForAD("","",""));
    }
}
本文链接:https://www.f2er.com/2684772.html

大家都在问