gpt4 book ai didi

c# - WPF 绑定(bind) DataGrid 中的 SelectedItem

转载 作者:太空狗 更新时间:2023-10-29 23:00:46 27 4
gpt4 key购买 nike

所以,我有一个绑定(bind)到项目列表的 TabControl(每个选项卡都是一个项目)- 工作正常。每个选项卡的内容是一个带有项目员工列表的 DataGrid - 也可以正常工作。现在,我想显示有关当前在 DataGrid 上选择的员工的一些信息。这是一些代码:主窗口.xaml 文件:

<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>

<DataTemplate x:Key="ContentTemplate">
<DataGrid ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList">

</DataGrid>

</DataTemplate>
</Window.Resources>

后来,我想通过简单地将其写入标签来测试此绑定(bind):

<Label Name="emp" Content="{Binding SelectedEmployee}"></Label>

和 MainWindowViewModel:

        public Employee SelectedEmployee { get { return selectedEmployee; }
set
{
if (selectedEmployee != value)
{
selectedEmployee = value;
NotifyPropertyChanged("SelectedEmployee");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

我是 WPF 的新手,我已经阅读了一些提示,但它们没有帮助。标签“emp”不显示任何内容。我错过了什么?

最佳答案

您没有通知您的属性已更改,试试这个

public Employee SelectedEmployee
{
get { return selectedEmployee; }
set
{
if (selectedEmployee != value)
{
selectedEmployee = value;
LastName = value;
NotifyPropertyChanged("SelectedEmployee"); //NotifyPropertyChanged("SelectedItem");
}
}
}

测试:

<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="763" Name="UI" >
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</Window.Resources>

<Grid>
<DataGrid ItemsSource="{Binding ElementName=UI,Path=Employees}" SelectedItem="{Binding ElementName=UI,Path=SelectedEmployee}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList" Margin="0,41,0,0" />
<Label Content="{Binding ElementName=UI,Path=SelectedEmployee.Name}" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Width="288" />
<Label Content="{Binding ElementName=employeesList,Path=SelectedItem.Name}" Height="28" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Width="288" Margin="294,0,0,0" />
</Grid>
</Window>

代码:

    public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();
private Employee _selectedEmployee;

public MainWindow()
{
InitializeComponent();
Employees.Add(new Employee { Name = "sa_ddam213" });
}

public ObservableCollection<Employee> Employees
{
get { return _employees; }
set { _employees = value; }
}

public Employee SelectedEmployee
{
get { return _selectedEmployee; }
set { _selectedEmployee = value; NotifyPropertyChanged("SelectedEmployee"); }
}


/// <summary>
/// Notifies the property changed.
/// </summary>
/// <param name="info">The info.</param>
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

public class Employee
{
public string Name { get; set; }
}

这似乎按预期工作,还是我遗漏了什么?

关于c# - WPF 绑定(bind) DataGrid 中的 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13755507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com