Silverlight Datagrid 块选择

发布于 2024-10-30 08:24:06 字数 466 浏览 3 评论 0原文

我们正在尝试向 Silverlight DataGrid 控件添加“块选择”:例如,用户应该能够选择从(第 4 列,第 5 行)到(第 6 列,第 8 行)的单元格矩形。

我们正在做的是保存选区的两个角,并通过设置单元格的背景颜色来直观地指示它。我们在滚动方面遇到了麻烦,因为单元格对象及其格式都会被回收。因此,您向上滚动,当选定的单元格从底部消失时,顶部的单元格条就会被着色!我尝试保存实际单元格对象的列表,并且“新”彩色单元格肯定是相同的 DataGridCell 实例,但内容当然不同。

我们可以通过可视化树来获取滚动条,因此我们最终可能会在垂直滚动条的 ValueChanged 事件处理程序中刷新选择显示。

但我想知道是否有更好的方法。我们不是 Silverlight 专家。有人尝试这样做吗?有没有什么对于 Silverlight 高手来说显而易见而我们甚至没有想到的?

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

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 ).

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-11-06 08:24:06

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

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.
}

请注意,indexselected 绑定上的转换器参数保存列的索引

 <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 绑定属性是否等于参数(即列的索引)

 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();
    }
}

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>

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文