更改gridview列的显示索引

时间:2013-05-16 06:45:51

标签: silverlight-5.0 telerik-grid

我们使用的是Telerik版本= 2012.2.725.1050

这里我在更改gridview列的DisplayIndex

时面临一个gridview问题

单击列,在单击“左键”焦点并且柱子完全移动后,但是当我们单击“右键”时,将移动柱,但不移动焦点,必须移动焦点。在这里我添加我的示例项目代码

提前感谢您的帮助

enter image description here

<UserControl x:Class="GridTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />           
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <telerik:RadGridView HorizontalAlignment="Left" x:Name="grdFormList" VerticalAlignment="Top" Grid.Column="0" Grid.Row="0" />        
    <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="1">
        <Button Content="Left" Height="23" HorizontalAlignment="Left"  Name="btnleft"  Width="75"   Margin="5,0,0,0" Click="btnleft_Click" />
        <Button Content="Right" Height="23" HorizontalAlignment="Left"  Name="btnright"  Width="75" Margin="5,0,0,0" Click="btnright_Click" />
    </StackPanel>
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows;
using System.ComponentModel;
using System.Globalization;
using System.Threading;


namespace GridTest
{
    public partial class MainPage : UserControl
    {
        public GridViewCellBase ClickedCell { get; set; }

        public MainPage()
        {
            InitializeComponent();
            this.grdFormList.AddHandler(GridViewCellBase.MouseLeftButtonDownEvent, new MouseButtonEventHandler(MouseDownOnCell), true);                

            grdFormList.Columns.Clear();
            for (Int32 attributelength = 0; attributelength < 10; attributelength++)
            {
                GridViewColumn textColumn = new GridViewColumn();
                // DataGridTextColumn textColumn = new DataGridTextColumn();
                textColumn.Header = string.Format("Column{0}", attributelength + 1);
                textColumn.UniqueName = string.Format("Uname{0}", attributelength + 1);
                textColumn.MinWidth = 5;
                textColumn.Width = 100;
                grdFormList.Columns.Add(textColumn);            
            }
        }
        private void MouseDownOnCell(object sender, MouseEventArgs args)
        {
            if (((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>() != null)
            {
                var aa = ((GridViewHeaderRow)(((GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>()).ParentOfType<GridViewHeaderRow>())).Cells;

                for (int i = 0; i < aa.Count; i++)
                {
                    ((GridViewCellBase)aa[i]).Background = new SolidColorBrush(Colors.Transparent);
                }

                this.ClickedCell = null;
                this.ClickedCell = (GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>();

                LinearGradientBrush brush = new LinearGradientBrush();
                brush.StartPoint = new Point(0.5, 0);
                brush.EndPoint = new Point(0.5, 1);

                GradientStop g1 = new GradientStop();
                g1.Color = Color.FromArgb(255, 227, 153, 54);
                brush.GradientStops.Add(g1);

                GradientStop g2 = new GradientStop();
                g2.Color = Color.FromArgb(255, 254, 211, 125);
                brush.GradientStops.Add(g2);

                ((GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>()).Background = brush;
            }
        }


        private void btnleft_Click(object sender, RoutedEventArgs e)
        {
            if (ClickedCell != null)
            {
                if (ClickedCell.Column != null)
                {
                    int i = ClickedCell.Column.DisplayIndex;
                    if (i >= 1)
                    {
                        ClickedCell.Column.DisplayIndex = i - 1;
                    }
                }
            }
        }

        private void btnright_Click(object sender, RoutedEventArgs e)
        {
            if (ClickedCell != null)
            {
                if (ClickedCell.Column != null)
                {
                    int k = ClickedCell.Column.DisplayIndex;
                    if (k <= grdFormList.Columns.Count - 1)
                    {
                        ClickedCell.Column.DisplayIndex = k + 1;
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我正在使用gridview的LayoutUpdated事件并重新分配一个属性并且它工作正常但不知道这是否是正确的做法

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;


namespace GridTest
{
    public partial class MainPage : UserControl
    {
        public GridViewCellBase ClickedCell { get; set; }
        private GridViewHeaderRow GVHeaderRow { get; set; }
        private string CellHeader { get; set; }


        public MainPage()
        {
            InitializeComponent();

            this.DataContext = this;

            this.grdFormList.AddHandler(GridViewCellBase.MouseLeftButtonDownEvent, new MouseButtonEventHandler(MouseDownOnCell), true);

            grdFormList.Columns.Clear();
            for (Int32 attributelength = 0; attributelength < 10; attributelength++)
            {
                GridViewColumn textColumn = new GridViewColumn();
                // DataGridTextColumn textColumn = new DataGridTextColumn();
                textColumn.Header = string.Format("Column{0}", attributelength + 1);
                textColumn.UniqueName = string.Format("Uname{0}", attributelength + 1);
                textColumn.MinWidth = 5;
                textColumn.Width = 100;
                grdFormList.Columns.Add(textColumn);
            }
        }
        private void MouseDownOnCell(object sender, MouseEventArgs args)
        {
            if (((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>() != null)
            {
                var aa = ((GridViewHeaderRow)(((GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>()).ParentOfType<GridViewHeaderRow>())).Cells;
                GVHeaderRow = ((GridViewHeaderRow)(((GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>()).ParentOfType<GridViewHeaderRow>()));

                for (int i = 0; i < aa.Count; i++)
                {
                    ((GridViewCellBase)aa[i]).Background = new SolidColorBrush(Colors.Transparent);
                }

                this.ClickedCell = null;
                this.ClickedCell = (GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>();

                CellHeader = ClickedCell.Column.Header.ToString();

                LinearGradientBrush brush = new LinearGradientBrush();
                brush.StartPoint = new Point(0.5, 0);
                brush.EndPoint = new Point(0.5, 1);

                GradientStop g1 = new GradientStop();
                g1.Color = Color.FromArgb(255, 227, 153, 54);
                brush.GradientStops.Add(g1);

                GradientStop g2 = new GradientStop();
                g2.Color = Color.FromArgb(255, 254, 211, 125);
                brush.GradientStops.Add(g2);

                ((GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>()).Background = brush;
            }
        }


        private void btnleft_Click(object sender, RoutedEventArgs e)
        {
            if (ClickedCell != null)
            {
                if (ClickedCell.Column != null)
                {
                    int i = ClickedCell.Column.DisplayIndex;
                    if (i >= 1)
                    {
                        ClickedCell.Column.DisplayIndex = i - 1;
                    }
                }
            }
        }

        private void btnright_Click(object sender, RoutedEventArgs e)
        {
            if (ClickedCell != null)
            {
                if (ClickedCell.Column != null)
                {
                    int k = ClickedCell.Column.DisplayIndex;
                    if (k < grdFormList.Columns.Count - 1)
                    {
                        ClickedCell.Column.DisplayIndex = k + 1;
                    }
                }
            }
        }

        private void grdFormList_LayoutUpdated(object sender, EventArgs e)
        {
            if (GVHeaderRow != null)
            {
                var aa = GVHeaderRow.Cells;
                for (int i = 0; i < aa.Count; i++)
                {
                    ((GridViewCellBase)aa[i]).Background = new SolidColorBrush(Colors.Transparent);
                }

                ////this.ClickedCell = null;
                ////this.ClickedCell = (GridViewCellBase)((UIElement)args.OriginalSource).ParentOfType<GridViewCellBase>();

                LinearGradientBrush brush = new LinearGradientBrush();
                brush.StartPoint = new Point(0.5, 0);
                brush.EndPoint = new Point(0.5, 1);

                GradientStop g1 = new GradientStop();
                g1.Color = Color.FromArgb(255, 227, 153, 54);
                brush.GradientStops.Add(g1);

                GradientStop g2 = new GradientStop();
                g2.Color = Color.FromArgb(255, 254, 211, 125);
                brush.GradientStops.Add(g2);

                ClickedCell = null;
                ClickedCell = GVHeaderRow.Cells.Where(a => a.Column.Header.ToString() == CellHeader).ToList()[0];

                ClickedCell.Background = brush;
            }
        }
    }
}