DatagridColumn CheckBox Bound Checked属性不工作
本文关键字:工作 属性 Checked CheckBox Bound DatagridColumn | 更新日期: 2023-09-27 18:02:40
我有一个绑定列到列表的数据网格(AnonymouseType),一个是使用DataGridTemplateColumn的复选框。CellTemplate,这是XAML:
<DataGrid Name="dgServicios" Margin="15" AutoGenerateColumns="False"
AlternatingRowBackground="#CCEEF6"
SelectionMode="Single" SelectionUnit="Cell" TabIndex="20" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Selected" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Selected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
HorizontalAlignment="Center" Click="CheckBox_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Codigo" Binding="{Binding Codigo}" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}" Width="Auto" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
CheckBox在运行时工作良好,但IsChecked属性不持续,当我试图从DB恢复值,项目布尔属性是ok的,但没有绑定复选框被选中。我怎么能得到复选框与项目布尔属性匹配??绑定是错的?
您能提供您绑定的对象列表的更多信息吗?
据我所知,你的绑定似乎是正确编码的。我尝试复制和粘贴你的xaml到一个新的解决方案,然后将ItemsSource属性设置为我创建的一个类,具有相同的列名,一切似乎工作正常。"GridItem"的ObservableCollection被绑定实现了INotifyPropertyChanged接口,并且任何时候属性得到更新,它都会通知UI。这就是我测试xaml:
所做的。MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<GridItem> GridItems;
public MainWindow()
{
InitializeComponent();
GridItems = new ObservableCollection<GridItem>();
GridItems.Add(new GridItem { Codigo = "value1", Nombre = "Value2", Selected = false });
GridItems.Add(new GridItem { Codigo = "value3", Nombre = "Value4", Selected = true });
GridItems.Add(new GridItem { Codigo = "value5", Nombre = "Value6", Selected = false });
dgServicios.ItemsSource = GridItems;
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
}
}
public class GridItem : INotifyPropertyChanged
{
private bool _Selected;
public bool Selected
{
get { return _Selected; }
set
{
_Selected = value;
OnPropertyChanged();
}
}
private string _Codigo;
public string Codigo
{
get { return _Codigo; }
set
{
_Codigo = value;
OnPropertyChanged();
}
}
private string _Nombre;
public string Nombre
{
get { return _Nombre; }
set
{
_Nombre = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
如果你能提供更具体的信息,我可能会给你一个更好的答案。