具有自定义控件的 DataGridTemplateColumn 单元格在更新后不显示



在我的项目中,我有一个自定义控件LessonCell。 Generic.xaml:

<Style TargetType="{x:Type local:LessonCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LessonCell}">
<Grid Background="{TemplateBinding Background}" Name="LessonGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.ColumnSpan="2" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center">
<Label Content="{TemplateBinding SubjectName}"/>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Left">
<Label Content="{TemplateBinding CabinetName}"/>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right">
<Label Content="{TemplateBinding TeacherName}"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

课程细胞.cs:

public class LessonCell : Control
{
static LessonCell()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LessonCell),
new FrameworkPropertyMetadata(typeof(LessonCell)));
}
public string TeacherName
{
get { return (string)GetValue(TeacherNameProperty); }
set { SetValue(TeacherNameProperty, value); }
}
public static readonly DependencyProperty TeacherNameProperty =
DependencyProperty.Register("TeacherName",
typeof(string), typeof(LessonCell), new PropertyMetadata(null));
public string SubjectName
{
get { return (string)GetValue(SubjectNameProperty); }
set { SetValue(SubjectNameProperty, value); }
}
public static readonly DependencyProperty SubjectNameProperty =
DependencyProperty.Register("SubjectName",
typeof(string), typeof(LessonCell), new PropertyMetadata(null));
public string CabinetName
{
get { return (string)GetValue(CabinetNameProperty); }
set { SetValue(CabinetNameProperty, value); }
}
public static readonly DependencyProperty CabinetNameProperty =
DependencyProperty.Register("CabinetName",
typeof(string), typeof(LessonCell), new PropertyMetadata(null));
}

我将此控件用作数据网格单元。为此,当我生成表的列时,我将其设置为 CellTemplate:

DataGridTemplateColumn lessonColumn = new DataGridTemplateColumn();
lessonColumn.Header = (i + 1) + " урок";
FrameworkElementFactory lessonCell = new FrameworkElementFactory(typeof(LessonCell));
lessonCell.SetBinding(BackgroundProperty, new Binding("COLOR_HEX[" + cellNumber + "]")
{
Mode = BindingMode.TwoWay,
NotifyOnTargetUpdated = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
lessonCell.SetBinding(LessonCell.SubjectNameProperty, new Binding("SUBJECT_NAME[" + cellNumber + "]")
{
Mode = BindingMode.TwoWay,
NotifyOnTargetUpdated = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
lessonCell.SetBinding(LessonCell.TeacherNameProperty, new Binding("TEACHER_NAME[" + cellNumber + "]")
{
Mode = BindingMode.TwoWay,
NotifyOnTargetUpdated = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
lessonCell.SetBinding(LessonCell.CabinetNameProperty, new Binding("CABINET_NAME[" + cellNumber + "]")
{
Mode = BindingMode.TwoWay,
NotifyOnTargetUpdated = true,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
lessonColumn.CellTemplate = new DataTemplate() { VisualTree = lessonCell };
ScheduleGrid.Columns.Add(lessonColumn);
multiBindingWidth.Bindings.Add(new Binding("ActualWidth") { Source = lessonColumn });

表的行使用我的自定义类型计划字符串存储在可观察集合中

private ObservableCollection<ScheduleString> ScheduleStringCollection = new ObservableCollection<ScheduleString>();
public class ScheduleString
{
public int SCHEDVARIANT_ID { get; set; }
public int CLASS_ID { get; set; }
public string CLASS_NAME { get; set; }
public int[] SCHEDSTRING_ID { get; set; }
public int[] STUDTIME_ID { get; set; }
public int[] LEARNCLASS_ID { get; set; }
public int[] TEACHER_ID { get; set; }
public string[] TEACHER_NAME { get; set; }
public int[] SUBJECT_ID { get; set; }
public string[] SUBJECT_NAME { get; set; }
public int[] CABINET_ID { get; set; }
public string[] CABINET_NAME { get; set; }
public string[] COLOR_HEX { get; set; }
}

通过 Itemsource,我将此集合分配给 ScheduleGrid。 当我在代码中更新集合时,更改将加载到表中,但不显示在 my 控件中。但是,如果双击包含已更改数据的单元格,则数据网格将显示它。如何解决?

尝试将模板中的TemplateBindings替换为普通绑定:

<Label Content="{Binding SubjectName, RelativeSource={RelativeSource AncestorType=local:LessonCell}}"/>

{TemplateBinding}是绑定的优化版本,具有一些限制。

我找到了解决问题的方法。在 ScheduleString 类中,有必要替换 ObservableCollection 上的数组

public class ScheduleChange
{
public int SCHEDVARIANT_ID { get; set; }
public int CLASS_ID { get; set; }
public string CLASS_NAME { get; set; }
public ObservableCollection<int> SCHEDCHANGE_ID { get; set; }
public ObservableCollection<int> STUDTIME_ID { get; set; }
public ObservableCollection<int> TEACHER_ID { get; set; }
public ObservableCollection<string> TEACHER_NAME { get; set; }
public ObservableCollection<int> SUBJECT_ID { get; set; }
public ObservableCollection<string> SUBJECT_NAME { get; set; }
public ObservableCollection<int> CABINET_ID { get; set; }
public ObservableCollection<string> CABINET_NAME { get; set; }
public ObservableCollection<int?> GROUP_ID { get; set; }
public ObservableCollection<DateTime> CHANGE_DATE { get; set; }
public ObservableCollection<string> COLOR_HEX { get; set; }
}

最新更新