如何使用c#从WPF数据模板中获取数据 [英] How to get Data from WPF Data Template with c#

查看:43
本文介绍了如何使用c#从WPF数据模板中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Data template with textboxes in my xaml wich getting data from an 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>

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

Now I want to get the displayed data on button click, how can I now get the values to my Click Event?

这就是我收藏的代码:

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

现在我需要将值安全地设置为在我的应用程序中使用它们每个 Active Directory 域都需要一个名称和密码,这正是我试图解决的问题.

And now I need to safe the values to settings to use them löater in my App Each active directory domain needs a name and passwort, thats what Im trying to solve.

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

to get a message for each founded item as validItem:

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

推荐答案

能否给出数据源的代码?这是在 Xaml 文件后面的代码中吗?根据您所展示的情况,情况似乎是这样.

Can you show the code for the data source? Is this in the code behind of the Xaml file? Based on what you have shown that would seem to be the case.

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

Assuming these assumptions are correct you would need to use the SelectedItem of the ListBox. So need to give the ListBox and x:Name. And in the event handler would use:

MyListBoxName.SelectedItem

获取物品

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

I do not know anything about setting up the Domain information using the Forest. But if you use the SelectedItem you get a SetCredentialsForAD object.

使用

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

Then in the click handler I can cast the SelectedItem to a SetCredentialsForAD object

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

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

SelectedItems is a List of SetCredentialsForAD objects. You need to cast the items in the List to the proper object type. Your code would not even compile, you cannot create a variable with the same name twice. To do what you want would be

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

这篇关于如何使用c#从WPF数据模板中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆