ich folgenden Code haben:Einstellung Datacontext in XAML in WPF
MainWindow.xaml
<Window x:Class="SampleApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding Employee}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="ID:"/>
<Label Grid.Row="1" Grid.Column="0" Content="Name:"/>
<TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding EmpID}" />
<TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding EmpName}" />
</Grid>
</Window>
Employee.cs
namespace SampleApplication
{
public class Employee
{
public Employee()
{
EmployeeDetails employeeDetails = new EmployeeDetails();
employeeDetails.EmpID = 123;
employeeDetails.EmpName = "ABC";
}
}
public class EmployeeDetails
{
private int empID;
public int EmpID
{
get
{
return empID;
}
set
{
empID = value;
}
}
private string empName;
public string EmpName
{
get
{
return empName;
}
set
{
empName = value;
}
}
}
}
Dies ist sehr einfach Code und ich nur Ich möchte die Eigenschaften EmpID
und EmpName
in meiner Klasse Employee.cs an Texteigenschaften binden von Textboxen in MainWindow.xaml, aber in meinen Textfeldern erscheint nichts, wenn ich den Code ausführe. Ist die Bindung richtig?
Auch jetzt Textbox bevölkert nicht. –
@nkp, haben Sie nach System.Data-Ausnahmen überprüft? Sie sollten Sie zu Tippfehlern führen. – BradleyDotNET
Ich habe noch nicht erfolgreich an das Ansichtsmodell in XAML gebunden, ohne die Assembly hinzuzufügen. Nur für das Bewusstsein angeben. 'xmlns: local =" clr-namespace: SampleApplication; assembly = SampleApplication "' – MistaGoustan