我有一个带有绑定列的Datagrid到一个List(Of AnonymouseType),一个是使用DataGridTemplateColumn.CellTemplate的CheckBox,这是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在运行时工作正常,但是当我尝试从DB恢复值时,IsChecked属性不会持久存在,项布尔属性正常,但没有选中Bound Checkbox。如何让Checkbox与项布尔属性匹配?绑定错了吗?
答案 0 :(得分:1)
您能否提供有关您绑定的对象列表的更多信息?
据我所知,您的绑定似乎编码正确。我尝试将您的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));
}
}
}
}
如果您提供更具体的信息,我可能会给您一个更好的答案。