2014-05-17 16 views
39

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?

Antwort

71

Dieser Code wird immer fehlschlagen.

Wie geschrieben, heißt es: "Suchen Sie in meiner DataContext-Eigenschaft nach einer Eigenschaft mit dem Namen" Employee ", und legen Sie sie auf die DataContext-Eigenschaft fest". Das ist eindeutig nicht richtig.

Code, um zu arbeiten, wie es ist, ändern Sie Ihre Fenster Erklärung:

<Window x:Class="SampleApplication.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SampleApplication" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <local:Employee/> 
</Window.DataContext> 

Dies erklärt einen neuen XAML-Namespace (local) und setzt die Datacontext auf eine Instanz der Klasse Employee. Dies führt dazu, dass Ihre Bindungen die Standarddaten (von Ihrem Konstruktor) anzeigen.

Es ist jedoch sehr unwahrscheinlich, dass dies tatsächlich das ist, was Sie wollen. Stattdessen sollten Sie eine neue Klasse (nennen wir es MainViewModel) mit einem Employee Eigenschaft, die Sie dann binden, wie folgt aus:

public class MainViewModel 
{ 
    public Employee MyEmployee { get; set; } //In reality this should utilize INotifyPropertyChanged! 
} 

Jetzt XAML wird:

<Window x:Class="SampleApplication.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:SampleApplication" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:MainViewModel/> 
    </Window.DataContext> 
    ... 
    <TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding MyEmployee.EmpID}" /> 
    <TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding MyEmployee.EmpName}" /> 

Jetzt können Sie andere hinzufügen Eigenschaften (anderer Typen, Namen) usw. weitere Informationen finden Sie Implementing the Model-View-ViewModel Pattern

+0

Auch jetzt Textbox bevölkert nicht. –

+0

@nkp, haben Sie nach System.Data-Ausnahmen überprüft? Sie sollten Sie zu Tippfehlern führen. – BradleyDotNET

+0

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

13

allererst Sie Eigentum mit Mitarbeiterdaten in der Employee Klasse erstellen sollten:

public class Employee 
{ 
    public Employee() 
    { 
     EmployeeDetails = new EmployeeDetails(); 
     EmployeeDetails.EmpID = 123; 
     EmployeeDetails.EmpName = "ABC"; 
    } 

    public EmployeeDetails EmployeeDetails { get; set; } 
} 

Wenn Sie das nicht tun, erstellen Sie Instanz des Objekts in Konstruktor und Sie verlieren Bezug auf es.

Im XAML sollten Sie eine Instanz der Klasse Employee erstellen, und danach können Sie sie DataContext zuweisen.

Ihre XAML sollte wie folgt aussehen:

<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" 
    xmlns:local="clr-namespace:SampleApplication" 
    > 
    <Window.Resources> 
     <local:Employee x:Key="Employee" /> 
    </Window.Resources> 
    <Grid DataContext="{StaticResource Employee}"> 
     <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 EmployeeDetails.EmpID}" /> 
     <TextBox Grid.Column="1" Grid.Row="1" Margin="3" Text="{Binding EmployeeDetails.EmpName}" /> 
    </Grid> 
</Window> 

Jetzt, nachdem Sie Eigentum mit Mitarbeiterdaten erstellt Sie, indem Sie diese Eigenschaft Bindung sollte:

Text="{Binding EmployeeDetails.EmpID}"