这是我的员工和公司类
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public Employee()
{
}
public Employee(int id, string name, string address)
{
this.ID = id;
this.Name = name;
this.Address = address;
}
}
public class Company
{
public string Name { get; set; }
public List<Employee> Employees { get; set; }
public Employee SelectedEmployee { get; set; }
public Company()
{
this.Name = "Test Technologies";
this.Employees = new List<Employee>();
this.Employees.Add(new Employee(1, "Prabodha", "Kottawa"));
this.Employees.Add(new Employee(2, "Prasad", "Rukmalgama"));
this.Employees.Add(new Employee(3, "Nisitha", "Nugegoda"));
this.Employees.Add(new Employee(4, "Danesh", "Kesbawa"));
this.Employees.Add(new Employee(5, "Jeewan", "Pannipitiya"));
this.SelectedEmployee = this.Employees.First();
}
}
我有一个名为EmployeeCompanyDetails的DataTemplate
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="EmployeeCompanyDetails">
<Grid>
<StackPanel Grid.Column="0" Background="Azure">
<TextBlock Text="~~ Employee Details ~~" />
<TextBlock x:Name="txtSelectedDetails" Text="{Binding View.CurrentCell.Item.ID, ElementName=DataGrid}" FontSize="20" FontWeight="SemiBold"/>
</StackPanel>
</Grid>
</DataTemplate>
这是我的MainWindow.xaml文件
<Window x:Class="DataTemplateTEst.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:DataTemplateTEst">
<Window.DataContext>
<local:Company/>
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/DataTemplateTEst;component/DataTemplates/EmployeeDetails.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Text="{Binding SelectedEmployee.Name}"/>
<DataGrid ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee,Mode=TwoWay}"/>
</StackPanel>
<ContentControl Grid.Column="1" DataContext="{Binding}" ContentTemplate="{StaticResource EmployeeCompanyDetails}"/>
</Grid>
我想要的是我想在txtSelectedDetails(DataTemplate中的TextBlock)中显示所选Employee的名称。由于以下代码段错误,因此无效。
<TextBlock x:Name="txtSelectedDetails" Text="{Binding View.CurrentCell.Item.ID, ElementName=DataGrid}" FontSize="20" FontWeight="SemiBold"/>