本文介绍了Silverlight Datagrid块选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正试图在Silverlight DataGrid控件中添加块选择:用户应该能够选择一个矩形区域,例如,从(col 4,row 5)到(col 6,row 8) 。

We're trying to add "block select" to the Silverlight DataGrid control: The user should be able to select, for example, a rectangle of cells from ( col 4, row 5 ) to ( col 6, row 8 ).

我们正在做的是保存所选内容的两个角,并通过设置单元格的背景颜色直观地进行指示。滚动会遇到麻烦,因为单元对象及其格式已被回收。因此,您向上滚动,当选定的单元格从底部消失时,顶部进入的单元格条会变成彩色!我尝试保存一个实际单元格对象的列表,并且新彩色单元格肯定是相同的DataGridCell实例,尽管其内容当然不同。

What we're doing is saving the two corners of the selection, and indicating it visually by setting the background color of the cells. We've run into trouble with scrolling, because the cell objects are recycled, along with their formatting. So you scroll up, and as the selected cells vanish off the bottom, bars of cells coming in at the top are colored! I've tried saving a List of the actual cell objects and the "new" colored cells are definitely the same DataGridCell instances, though with different content of course.

我们能够通过可视树将手放在滚动条上,因此我们最终可能会在ValueChanged事件处理程序中为垂直显示刷新选择显示。滚动条。

We're able to get our hands on the scrollbars via the visual tree, so we'll may end up refreshing the selection display in a ValueChanged event handler for the vertical scrollbar.

但是我想知道是否有更好的方法。我们不是Silverlight专家。有没有人尝试这样做? Silverlight天才有什么我们根本没有想到的东西吗?

But I'd like to know if there's a better way. We're not Silverlight experts. Has anybody tried to do this? Is there anything obvious to a Silverlight whiz that we're not even thinking of?

我们不会购买任何东西。不幸的是,由于公司官僚主义的原因,这不是一种选择。

We're not going to buy anything. For corporate bureaucracy reasons, unfortunately, that's not an option.

推荐答案

为什么不在视图模型中包括它。我要做的是创建一个交互的嵌套可枚举视图模型,即,如果将数据网格绑定到T的IEnumerable,其中T是表示每一行的视图模型,则id在该视图模型上具有类似IndexSelected的内容。
然后使用某种valueconverter将id绑定到该indexselected属性的背景色

Why not include that in you viewmodel. What i would do is create a nested enumerable viewmodel of the interaction, ie if the datagrid is bound to a IEnumerable of T where T is a viewmodel representing each row, id have something like IndexSelected on that viewmodel.Then id bind the back color using a valueconverter of some sort to that indexselected property,

public class RowViewModel
{
   public string Col1 { get; set; }
   public string Col2 { get; set; }
   public string Col3 { get; set; }

   public int IndexSelected { get; private set; }

   //Id also make a command here or something to set the indexselected but ill leave that for you :)

}

public class GridViewModel
{
    public ObservableCollection<RowViewModel> Rows; // Bound to Datagrid.ItemsSource.
}

请注意,索引选择绑定上的转换器参数将保留列的索引

Notice the converter param on the indexselected binding holds the index of the column

 <sdk:DataGrid>
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn Header="Col1">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Background="{Binding IndexSelected, Converter={StaticResource IndexToColorConverter}, ConverterParameter=1}">
                            <TextBlock Text="{Binding Col1}" />
                        </Grid>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn Header="Col2">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Background="{Binding IndexSelected, Converter={StaticResource IndexToColorConverter}, ConverterParameter=2}">
                            <TextBlock Text="{Binding Col2}" />
                        </Grid>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

所有转换器将要做的就是检查indexselected绑定属性是否等于参数(即索引)

and all the converter will do is check if the indexselected bound property equals the parameter (which is the index of the column)

 public class IndexToColorConverter : IValueConverter
 {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
         if (value == parameter)
        {
            return new SolidColorBrush(Colors.Red);
        }
        return new SolidColorBrush(Colors.White);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这篇关于Silverlight Datagrid块选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:11