【问题标题】:How to get Data from WPF Data Template with c#如何使用 c# 从 WPF 数据模板中获取数据
【发布时间】:2020-01-28 14:30:46
【问题描述】:

我的 xaml 中有一个带有文本框的数据模板,它从 observablecollection 中获取数据。

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

    <DataTemplate x:Key="NameItemTemplate" x:Name="CredentialsofDomains">
        <Border Name="BorderControl" Background="Transparent" BorderThickness="0" Padding="0">
            <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}" Margin="0,0,40,0"></TextBlock>
            <Label Grid.Row="1" Grid.Column="1" Content="samAccountName"></Label>
            <Label Grid.Row="1" Grid.Column="2" Content="Passwort"></Label>
            <TextBox x:Name="samAccountNameForCredentials" Grid.Row="2" Grid.Column="1" Text="{Binding Path=LoginName}" Width="100" />
                <TextBox x:Name="passwordForCredentials" Grid.Row="2" Grid.Column="2" Text="{Binding Path=PassWord}" Width="100"/>
        </Grid>
        </Border>
    </DataTemplate>
                              <ListBox Width="auto"
                                 ItemsSource="{Binding Source={StaticResource NameListData}}"  
                                 ItemTemplate="{StaticResource NameItemTemplate}"  
                                 IsSynchronizedWithCurrentItem="True" />
                                <Fluent:Button Name="saveDomainCredentials" SizeDefinition="Large" VerticalAlignment="Bottom" HorizontalAlignment="Right" Header="Speichern" Click="SaveDomainCredentials_Click"></Fluent:Button>

现在我想获取按钮点击时显示的数据,我现在如何获取点击事件的值?

这就是我收藏的代码:

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(objDomain.ToString(), "", ""));
        }
    }
}

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

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

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

现在我需要将这些值保存到设置中,以便稍后在我的应用程序中使用它们 每个活动目录域都需要一个名称和密码,这就是我要解决的问题。

为每个已建立的项目获取一条消息作为validItem:

private void SaveDomainCredentials_OnClick(object sender, RoutedEventArgs e)
{
    foreach (var item in ListBox.SelectedItems.OfType<SetCredentialsForAD>())
    {
        MessageBox.Show("Domaine: " + item.DomaineName);
    }
}

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您能显示数据源的代码吗?这是在 Xaml 文件后面的代码中吗?根据您所展示的情况,情况似乎如此。

    假设这些假设是正确的,您将需要使用 ListBox 的 SelectedItem。所以需要给ListBox和x:Name。并且在事件处理程序中会使用:

    MyListBoxName.SelectedItem
    

    获取物品

    我对使用森林设置域信息一无所知。但是如果你使用 SelectedItem 你会得到一个 SetCredentialsForAD 对象。

    使用

        public NameList() : base()
        {
    
            Add(new SetCredentialsForAD("domain 1", "name 1", "pwd 1"));
            Add(new SetCredentialsForAD("domain 2", "name 2", "pwd 2"));
            Add(new SetCredentialsForAD("domain 3", "name 3", "pwd 3"));
            Add(new SetCredentialsForAD("domain 4", "name 4", "pwd 4"));
        }
    

    然后在点击处理程序中,我可以将 SelectedItem 转换为 SetCredentialsForAD 对象

        if ( ListBox.SelectedItem is SetCredentialsForAD item )
        {
            Console.WriteLine(item.DomaineName);
            Console.WriteLine(item.LoginName);
            Console.WriteLine(item.PassWord);
        }
    

    SelectedItems 是 SetCredentialsForAD 对象的列表。您需要将 List 中的项目转换为正确的对象类型。您的代码甚至无法编译,您不能两次创建具有相同名称的变量。做你想做的事

        private void SaveDomainCredentials_OnClick(object sender, RoutedEventArgs e)
        {
            foreach (var item in ListBox.SelectedItems.OfType<SetCredentialsForAD>())
            {
                MessageBox.Show("Domaine: " + item.DomaineName);
            }
        }
    

    【讨论】:

    • 通过这种方法,我得到了 SetCredentialsForAD 类的名称,比之前更多:) 但不是我需要的全部。
    • Joe H. 给了我想要的数据,非常感谢。但是,如果我现在点击列表下的按钮,以保存整个数据,我已经在每个域的文本框中输入,我只收到 domain1 的第一项。
    • 您正在使用 ListBox,因此一次只能选择一项。如果您更改所选项目,您将获得该项目(即 2 或 3)。您可以设置一个 ListBox 以允许选择多个项目。在这种情况下,您将使用 SelectedItems 属性并遍历项目
    • 我已经设置了多选,现在我可以在我的列表框中多选条目。现在我尝试使用 foreach 将每个列表条目存储到数据库中,我已经在上面发布了我的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2014-12-03
    • 2012-01-22
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多