【问题标题】:How can I add a new entry to my ObservableCollection如何向我的 ObservableCollection 添加新条目
【发布时间】:2020-01-30 11:49:42
【问题描述】:

我正在使用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>

我需要在单击列表中添加一个新项目以在我的 UI 中显示它并用它做其他事情。

我尝试了一些添加空列表项的方法,例如:

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

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

谢谢

【问题讨论】:

  • 您没有描述任何问题。 UI 中的列表是否仍为空白?当您使用调试器查看项目时,是否不会在代码中添加项目?你设置了DataContext吗?帮助我们帮助您。

标签: c# wpf xaml observablecollection


【解决方案1】:

我会研究如何在您的应用程序中使用 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("", "", ""));
    }
}

【讨论】:

  • 这就是缺少的东西。我现在明白我需要更多关于 MVVM 以及如何解决此类问题的知识。你有什么好的资源给我吗?谢谢你和最好的问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
  • 2018-12-23
  • 2011-09-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多