问题
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?
Thats the Code Behind for my collection:
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; }
}
}
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);
}
}
回答1:
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.
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
To get the Item
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.
Using
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"));
}
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 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);
}
}
来源:https://stackoverflow.com/questions/59950984/how-to-get-data-from-wpf-data-template-with-c-sharp