C# 数据网格SelecteItem的WPF DataTemplate Textblock绑定
这是我的员工和公司课程C# 数据网格SelecteItem的WPF DataTemplate Textblock绑定,c#,wpf,C#,Wpf,这是我的员工和公司课程 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) {
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的数据模板
<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>
我想要的是在DataTemplate的txtSelectedDetailsTextBlock中显示所选员工的姓名。因为下面的代码段是错误的,所以这不起作用
<TextBlock x:Name="txtSelectedDetails" Text="{Binding View.CurrentCell.Item.ID, ElementName=DataGrid}" FontSize="20" FontWeight="SemiBold"/>
为什么要绑定到网格的当前单元格id?您可以{Binding SelectedItem,ElementName=myGrid}这是我的示例代码,我没有下载您的代码,这可以在这里轻松解决。你试过我的建议了吗?你明白了吗?我刚刚添加了Text={Binding View.CurrentCell.Item.ID,ElementName=DataGrid}代码用于测试目的。我想要的是在TextBlock txtSelectedDetails上显示所选的行ID、名称或地址。据我所知,我必须为文本设置正确的属性路径。我已经将该属性路径设置为View.CurrentCell.Item.ID,我认为该属性路径是错误的。啊!!是的,莫蒂,成功了。。非常感谢你的帮助