积极答复者
斑竹看过来,关于datagrid控件有个问题

问题
-
http://filteringdatagrid.codeplex.com/releases/31984/download/80579
这个地方可以用个FilterDataGrid控件,我看了例子很好用,但是我在源代码中稍作修改点击排序和filter还有外面的scrollbar都不好用了,排序时间变成,scrollbar消失,很怪,我只是在上面想加个条件选择过滤的查询功能:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Labs.Filtering" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="Labs.Filtering.Window1" Title="Window1" Height="468" Width="784" mc:Ignorable="d"> <Canvas> <GroupBox Height="61" Header="查询条件" Width="762"> <Canvas> <Button Name="btnErroOnly" Height="22" Canvas.Left="6" Canvas.Top="2" Width="68" >仅显示错误</Button> <Button Canvas.Left="89" Canvas.Top="2" Height="22" Name="btnWarningOnly" Width="68" >仅显示警告</Button> </Canvas> </GroupBox> <Grid></Grid> <local:FilteringDataGrid IsFilteringCaseSensitive="False" ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="False" x:Name="dataGrid1" Height="234" Canvas.Left="0" Canvas.Top="67" Width="531"> <my:DataGrid.Columns> <my:DataGridTextColumn Header="事件号" Binding="{Binding Path=Name}" /> </my:DataGrid.Columns> </local:FilteringDataGrid> </Canvas> </Window>
原来的代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Labs.Filtering"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="Labs.Filtering.Window1"
Title="Window1" Height="468" Width="784" mc:Ignorable="d">
<Grid>
<local:FilteringDataGrid IsFilteringCaseSensitive="False" ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="True" x:Name="dataGrid1"/>
</Grid>
</Window>原来的好用,我家了点东西后发现scrollbar没有了,排序的点击花时间也长了,why?
答案
-
这是个第三方作者自己修改的控件,其实我看了下他的代码,他有个限制,只有当Column Header与属性同名才可以过滤。你可以从FilterDataGrid.cs 129行:
object property = GetPropertyValue(item, filter.Key);
columnFilters的Key来自于当前ColumnHeader的DataContext,在你的情况下就是Header Name。
所以你也只有当这个Column header为Name时候才可以过滤。
<local:FilteringDataGrid IsFilteringCaseSensitive="False" ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="False" x:Name="dataGrid1" Height="234" Canvas.Left="0" Canvas.Top="67" Width="531">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=TwoWay}" />
</my:DataGrid.Columns>
</local:FilteringDataGrid>
或者你可以修改他的代码成这样子:
private void UpdateFilter(TextBox textBox, DataGridColumnHeader header)
{
// Try to get the property bound to the column.
// This should be stored as datacontext.
// string columnBinding = header.DataContext != null ? header.DataContext.ToString() : "";
string columnBinding = ((System.Windows.Data.Binding)(((Microsoft.Windows.Controls.DataGridBoundColumn)(header.Column)).Binding)).Path.Path;
// Set the filter
if (!String.IsNullOrEmpty(columnBinding))
columnFilters[columnBinding] = textBox.Text;
}
至于滚动条和其他,我这边没有重现,不过看上去像是WPF Toolkit DataGrid的bug,我以前在英文论坛也看到有人反馈过,是与性能问题相关,不过.Net 4 里面的DataGrid就没有这个问题。所以你可以大胆的将它的Toolkit DataGrid用WPF 4的DataGrid来替换。
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已建议为答案 Jie BaoModerator 2011年9月19日 18:16
- 已编辑 Jie BaoModerator 2011年9月19日 18:21
- 已标记为答案 Jie BaoModerator 2011年9月26日 3:25