【问题标题】:DataGridTemplateColumn cell with Custom Control not displayed after updating更新后不显示带有自定义控件的 DataGridTemplateColumn 单元格
【发布时间】: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


    【解决方案1】:

    尝试用模板中的普通绑定替换TemplateBindings

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

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

    【讨论】:

      【解决方案2】:

      我找到了解决问题的方法。在 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; }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-29
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多