none
how to add some value to each of columns dynamic? RRS feed

  • Question

  • hello everyone.

    i want to add some value to each of columns dynamic?

    so i tried to make a some code to do this, but there is some problem.

    i have made listview in Code, and i also have made each of columns dynamic like below.


                ParameterValueCollection parameterCollection = new ParameterValueCollection();<-- this is ObservableCollection class
                ParametersDisplayView parameterDisplayView = new ParametersDisplayView();
                Thickness thickness = new Thickness();
                thickness.Left = 5;
                thickness.Top = 5;
                thickness.Right = 0;
                thickness.Bottom = 0;
                parameterDisplayView.Margin = thickness;

                parameterDisplayView.SelectionMode = SelectionMode.Single;
                ScrollViewer scrollViewer = new ScrollViewer();
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                scrollViewer.CanContentScroll = true;
                parameterDisplayView.Name = "LogDisplayView";
                parameterDisplayView.ItemsSource = parameterCollection;

                VirtualizingStackPanel.SetIsVirtualizing(parameterDisplayView, true);
                ScrollViewer.SetHorizontalScrollBarVisibility(parameterDisplayView, ScrollBarVisibility.Auto);
                ScrollViewer.SetVerticalScrollBarVisibility(parameterDisplayView, ScrollBarVisibility.Auto);
                ScrollViewer.SetCanContentScroll(parameterDisplayView, true);
                parameterDisplayView.Background = (Brush)Application.Current.Resources["PanelGradientBrush"];
                parameterDisplayView.ItemContainerStyle = (Style)Resources["ParametersEditListViewItemStyle"];
               
              
                GridView gridView = new GridView();
                gridView.AllowsColumnReorder = true;

                GridViewColumn emptyColumn = new GridViewColumn();
                emptyColumn.Width = 10;
                emptyColumn.HeaderContainerStyle = (Style)Resources["FirstColumnGridViewColumnHeader"];
                emptyColumn.CellTemplate = (DataTemplate)Resources["EmptyColumnTemplate"];
                gridView.Columns.Add(emptyColumn);

                SortListViewColumn sortColumnIndex = new SortListViewColumn();
                sortColumnIndex.SetValue(SortListViewColumn.SortPropertyProperty, "Time");
                sortColumnIndex.SetValue(SortListViewColumn.HeaderProperty, "Time");
                sortColumnIndex.Width = 200;
                sortColumnIndex.HeaderContainerStyle = (Style)Application.Current.Resources["ParametersGridViewColumnHeader"];
                sortColumnIndex.CellTemplate = (DataTemplate)Resources["TimeColumnTemplate"];
                gridView.Columns.Add(sortColumnIndex);


    // This function will be called depends on items, so i have made each of columns
            private void MakeParameterListViewHeader(ParametersDisplayView parameterDisplayView, GridView gridView, LogParameter logParameter)
            {
                SortListViewColumn sortColumnIndex = new SortListViewColumn();
                sortColumnIndex.SetValue(SortListViewColumn.SortPropertyProperty, logParameter.ParameterName);
                sortColumnIndex.SetValue(SortListViewColumn.HeaderProperty, logParameter.ParameterName);
                sortColumnIndex.Width = 200;
                sortColumnIndex.HeaderContainerStyle = (Style)Application.Current.Resources["ParametersGridViewColumnHeader"];
                sortColumnIndex.CellTemplate = (DataTemplate)Resources["ParameterNameColumnTemplate"];
               
                gridView.Columns.Add(sortColumnIndex);
            }


    <XAML>
            <DataTemplate x:Key="EmptyColumnTemplate">
            </DataTemplate>


            <DataTemplate x:Key="TimeColumnTemplate">
                <TextBlock Name="LogTime" HorizontalAlignment="Left" Text="{Binding Mode=OneTime,Path=LogTime}" Foreground="{DynamicResource FontColor}"  Margin="5, 0, 0, 0" />
            </DataTemplate>

            <DataTemplate x:Key="ParameterNameColumnTemplate">
                <TextBlock Name="ParameterValue" HorizontalAlignment="Left"  Text="" Foreground="{DynamicResource FontColor}" Margin="5, 0, 0, 0" />
            </DataTemplate>

    after listview made with each of columns, i want to update each of columns with some value like below

                            parameterDisplayView.View = gridView;
                            var delParameters = (from Item in parameterCollection
                                                 where Item.LogTime == parameter.LogTime
                                                 select Item).Count();

                            if (delParameters == 0)
                            {
                                parameterCollection.Add(parameter);
                                AddLogParameterToListView(ref rowCount, parameterDisplayView, gridView, parameter); ==> i want to update each of columns from here.
                            }

            private void AddLogParameterToListView(ref int rowCount,ParametersDisplayView parameterDisplayView, GridView gridView, LogParameter logParameter)
            {
                TextBlock tb = (TextBlock)GetElementFromCellTemplate(parameterDisplayView, 2, rowCount, "ParameterValue");==> The TextBlock has always null.(here is problem)
                if (tb != null)
                {
                    tb.Text = logParameter.LogTime;
                    rowCount++;
                }
            }

            public FrameworkElement GetElementFromCellTemplate(ListView listView, Int32 column, Int32 row, String name)
            {
                if (row >= listView.Items.Count || row < 0)
                {
                    throw new ArgumentOutOfRangeException("row");
                }
                GridView gridView = listView.View as GridView;
                if (gridView == null) { return null; } if (column >= gridView.Columns.Count || column < 0)
                {
                    throw new ArgumentOutOfRangeException("column");
                }
                ListViewItem item = listView.ItemContainerGenerator.ContainerFromItem(listView.Items[row]) as ListViewItem;
                if (item != null)
                {
                    GridViewRowPresenter rowPresenter = GetFrameworkElementByName<GridViewRowPresenter>(item);
                    if (rowPresenter != null)
                    {
                        ContentPresenter templatedParent = VisualTreeHelper.GetChild(rowPresenter, column) as ContentPresenter;
                        DataTemplate dataTemplate = gridView.Columns[column].CellTemplate;
                        if (dataTemplate != null && templatedParent != null)
                        {
                            return dataTemplate.FindName(name, templatedParent) as FrameworkElement;
                        }
                    }
                } return null;
            }

    How can i do to solve this problem?

    i want to make a listview with some function like below

    Time | Column1 | Column2 | ------- | Column n
    Time | Value      |  Value     | ------  |  Value

    A number of column will be changed depends on log Data.
    i need this kind of listview.

    Thank you ...


    lightdoll
    Monday, June 29, 2009 6:48 PM