如何解决WPF DataGridCellInfo DatePicker列的Item属性值
在下面的WPF DataGrid
中,以下代码成功返回所选行第一列中单元格的值。
对于第一个单元格,下面的数据项DataGridCellInfo.Item是一个TextBlock(默认值):
DataGridCellInfo cellInfo = dgEvents.SelectedCells[0]; //cell in first column in selected row
string sSubject = (cellInfo.Column.GetCellContent(cellInfo.Item) as TextBlock).Text;
问题:下面的数据项cellInfo.Item
应该是什么?该单元格确实具有日期值,但出现错误:cellInfo_1.Column.GetCellContent(cellInfo_1 .Item) as DatePicker returns null
DataGridCellInfo cellInfo_1 = dgEvents.SelectedCells[1]; //cell in second column in selected row
DateTime? mydatetime = (cellInfo_1.Column.GetCellContent(cellInfo_1 .Item) as DatePicker).SelectedDate;
备注:请注意,问题是要获取单元格中选定的新日期值,而不是有关单元格已绑定的旧值。
XAML :
<Window x:Class="MyWPFProject.DataGrid_control.DataGridColumnsSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataGridColumnsSample" Height="200" Width="300">
<Grid Margin="10">
<DataGrid Name="dgUsers" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Birthday">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Birthday}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
解决方法
您根本不需要通过C#代码与WPF中的控件进行交互。
相反,您可以将DataGrid
绑定到基础集合并与该集合进行交互。
这是一个简单的示例,它如何工作。
数据类
public class User : INotifyPropertyChanged
{
private string _name;
private DateTime _birthday = DateTime.Today;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
}
public DateTime Birthday
{
get => _birthday;
set
{
_birthday = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
如果更改了某些属性,则 INotifyPropertyChanged
可以注释DataGrid
。 DataGrid
将自动更新其布局。
隐藏代码
public partial class MainWindow : Window,INotifyPropertyChanged
{
private ObservableCollection<User> _users;
public ObservableCollection<User> Users
{
get => _users;
set
{
_users = value;
OnPropertyChanged();
}
}
private ICollectionView usersView;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender,RoutedEventArgs e)
{
User selectedUser = usersView.CurrentItem as User;
MessageBox.Show(selectedUser.Name + Environment.NewLine + selectedUser.Birthday);
}
private void Window_Loaded(object sender,RoutedEventArgs e)
{
InitUsers();
}
private void InitUsers()
{
Users = new ObservableCollection<User>();
Users.Add(new User { Name = "John",Birthday = DateTime.Now });
Users.Add(new User { Name = "Jane",Birthday = DateTime.Now });
usersView = CollectionViewSource.GetDefaultView(Users);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
您可以从集合中添加/删除项目,重新分配Users
属性,并以编程方式对数据进行任何处理。 DataGrid
将与数据保持同步。
UI
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBox Margin="5" Width="300" Text="{Binding Users/Name,UpdateSourceTrigger=PropertyChanged}"/>
<DatePicker SelectedDate="{Binding Users/Birthday,UpdateSourceTrigger=PropertyChanged}" Margin="5"/>
<Button Content="Show selected" Click="Button_Click" Margin="5" Padding="10,0"/>
</StackPanel>
<DataGrid Grid.Row="1" Margin="5" AutoGenerateColumns="False" ItemsSource="{Binding Users}" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="*"/>
<DataGridTemplateColumn Header="Birthday" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Birthday,UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
确实,这不是实现“选定项”的唯一方法,但可以肯定地将这种确切方法直接用在零行C#代码的UI中。
进一步建议的步骤是了解MVVM编程模式。
试玩示例,在有和没有UpdateSourceTrigger
s的情况下尝试
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。