【发布时间】:2018-10-30 11:30:14
【问题描述】:
在我的项目中,我有一个自定义控件 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>
LessonCell.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));
}
我将此控件用作 Datagrid 单元格。为此,当我生成表格的列时,我将其设置为 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 });
使用我的自定义类型 ScheduleString 将表的行存储在 ObservableCollection 中
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。 当我在我的代码中更新集合时,更改会加载到表中,但不会显示在我的控件中。但是,如果您双击具有更改数据的单元格,Datagrid 会显示它。如何解决?
【问题讨论】:
标签: c# wpf datagrid custom-controls observablecollection