WPF数据网格甘特历

时间:2017-11-25 09:52:22

标签: c# wpf wpfdatagrid

我使用datagrid DataGridTemplateColumn HeaderTemplate是自定义用户控件来生成我的时间轴,并且该控件填充整个datagrid单元格以获得所需的结果。但是通过这个解决方案,我得到了选择问题,在选择单元格边框时是不可见的。为了克服问题,用popup做了一个临时解决方案。 enter image description here

但我感兴趣的是如何在我的自定义HeaderTemplate上设置选区边框以获得图片中的结果。enter image description here

提前致谢。

public  void Init()
    {
        gridKalendar.AutoGenerateColumns =false;
        gridKalendar.EnableColumnVirtualization = true;
        gridKalendar.EnableRowVirtualization = true;
        gridKalendar.CanUserAddRows = false;
        gridKalendar.CanUserReorderColumns = false;
        gridKalendar.CanUserSortColumns = false;
        gridKalendar.CanUserResizeColumns = false;

        gridKalendar.CanUserResizeRows =false;
        gridKalendar.SetValue(VirtualizingPanel.VirtualizationModeProperty, VirtualizationMode.Recycling);
        gridKalendar.SetValue(VirtualizingPanel.IsVirtualizingWhenGroupingProperty, false);
        gridKalendar.SetValue(VirtualizingPanel.IsVirtualizingProperty, true);
        gridKalendar.SetValue(ScrollViewer.CanContentScrollProperty, false);
        gridKalendar.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
        gridKalendar.SelectionMode = DataGridSelectionMode.Extended;

        gridKalendar.GridLinesVisibility = DataGridGridLinesVisibility.Horizontal;
        gridKalendar.FrozenColumnCount = 3;
        gridKalendar.SelectionUnit = DataGridSelectionUnit.Cell;

        var klikkonverter = new KlikConverter();
        var converter = new BackGroundConverter();
        var vrednostconverter = new VrednostConverter();
        var hderkonverter = new HederConverter();
        var godina = DateTime.Now.Year;
        var detaljiKonverter = new DetaljiConverter();
        var vizuelizacijaTastera = new VizuelizacijaTasteraConverter();
        var brojBKkonverter = new BrojBukingaConverter();
        var brojRezervacijeKonverter = new BrojRezervacijeConverter();
        var klikZaKontrolu = new KlikZaKontroluConverter();

        DateTime DatumPocetakKalendar = (DateTime)PostaviKalendarNaDanPocetak;
        DateTime DatumKrajKalendar = (DateTime)PostaviKalendarNaDanKraj;

        if (DatumPocetakKalendar != null && DatumKrajKalendar != null)
        {
            while (DatumPocetakKalendar < DatumKrajKalendar)
            {
                #region poboljsani tem

                DataGridTemplateColumn kolona = new DataGridTemplateColumn();
                kolona.Header = string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar);

                DataTemplate dataTemplejt = new DataTemplate();
                DataTemplate dataTemplejtHeder = new DataTemplate();
                FrameworkElementFactory kalendarKRK = new FrameworkElementFactory(typeof(KalendarKRK));
                FrameworkElementFactory kalendarKRKdatum = new FrameworkElementFactory(typeof(KalendarKRKDatum));


                #region Generisanje celije

                kalendarKRK.SetValue(KalendarKRK.ImeGostaPropery, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)){ Converter = vrednostconverter });

                kalendarKRK.SetValue(KalendarKRK.BojaKalendaraProperty, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter = converter });

                kalendarKRK.SetValue(KalendarKRK.PrikazTasteraDetaljaProperty, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter = detaljiKonverter });

                kalendarKRK.SetValue(KalendarKRK.VizuelizacijaTasteraProperty, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter = vizuelizacijaTastera });

                kalendarKRK.SetValue(KalendarKRK.BrojBukingaProperty, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter = brojBKkonverter });

                kalendarKRK.SetValue(KalendarKRK.BrojRezervacijeProperty, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter =brojRezervacijeKonverter });

                kalendarKRK.SetValue(KalendarKRK.OstaleKoloneKlikPropery, new Binding(string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar)) { Converter = klikZaKontrolu });

                #endregion

                #region Generisanje hedera sa datumima

                kalendarKRKdatum.SetValue(KalendarKRKDatum.CeoDatumProperty,string.Format("{0:yyyy-MM-dd}", DatumPocetakKalendar.Date).ToString());//yyyy/MM/dd
                kalendarKRKdatum.SetValue(KalendarKRKDatum.DanProperty, string.Format("{0:dddd}", DatumPocetakKalendar.Date).ToString());



                #endregion

                #region stilovi

                kolona.HeaderStyle = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader));

                kolona.HeaderStyle.Setters.Add(new Setter{ Property = System.Windows.Controls.Primitives.DataGridColumnHeader.BackgroundProperty, Value = ObojiVikende(DatumPocetakKalendar)});

                kolona.HeaderStyle.Setters.Add(new Setter {Property = DataGridColumnHeader.MarginProperty, Value = new Thickness(0) });


                kolona.HeaderStyle.Setters.Add(new Setter{Property = DataGridColumnHeader.BorderThicknessProperty, Value = new Thickness(0)});
                kolona.HeaderStyle.Setters.Add(new Setter{Property = DataGridColumnHeader.VerticalContentAlignmentProperty, Value =VerticalAlignment.Stretch });
                kolona.HeaderStyle.Setters.Add(new Setter{Property = DataGridColumnHeader.HorizontalContentAlignmentProperty, Value = HorizontalAlignment.Stretch });
                kolona.HeaderStyle.Setters.Add(new Setter{Property = DataGridColumnHeader.VerticalAlignmentProperty,Value = VerticalAlignment.Stretch });



                kolona.HeaderStyle.Setters.Add(new Setter {Property = DataGridColumnHeader.HorizontalAlignmentProperty,Value = HorizontalAlignment.Stretch});

                kolona.CellStyle = new Style(typeof(DataGridCell));
                kolona.CellStyle.Setters.Add(new Setter{Property = DataGridCell.BorderThicknessProperty,Value = new Thickness(0)});

                kolona.CellStyle.Setters.Add(new Setter{Property = DataGridCell.MarginProperty,Value = new Thickness(0)});
                kolona.CellStyle.Setters.Add(new Setter{Property = DataGridCell.VerticalAlignmentProperty,Value = VerticalAlignment.Stretch });
                kolona.CellStyle.Setters.Add(new Setter {Property = DataGridCell.HorizontalAlignmentProperty,Value = HorizontalAlignment.Stretch });

                #endregion


                #region Zumiranje


                //Binding bind = new Binding();// {Path=System.Windows.Controls.Slider.ValueProperty.ToString(), ElementName=ZumiranjeKolone.Name};
                //bind.Source = ZumiranjeKolone;
                //bind.Path = new PropertyPath(System.Windows.Controls.ComboBox.SelectedValueProperty);
                //ScaleTransform st = new ScaleTransform();


                //BindingOperations.SetBinding(dataGridLength.Value, WidthProperty, bind);

                //BindingOperations.SetBinding(st, ScaleTransform.ScaleXProperty, bind);
                //BindingOperations.SetBinding(st, ScaleTransform.ScaleYProperty, bind);

                double w = 80;

                kalendarKRK.SetValue(KalendarKRK.ZumiranjePropery,w);

                kalendarKRKdatum.SetValue(KalendarKRKDatum.ZumiranjePropery, w);

                kolona.Width = DataGridLength.SizeToCells;            



                #endregion

                dataTemplejtHeder.VisualTree = kalendarKRKdatum;
                dataTemplejt.VisualTree = kalendarKRK;

                kolona.CellTemplate = dataTemplejt;


                kolona.HeaderTemplate = dataTemplejtHeder;                                                                                             


                gridKalendar.Columns.Add(kolona);
                #endregion


                DatumPocetakKalendar = DatumPocetakKalendar.AddDays(1);

            }
        }

0 个答案:

没有答案