l find your current examples to be FAR too complicated. (MVVM, date ranges, etc)
I just need a simple example to show how to create a custom Text filter with
Contains
Starts with
Ends with
It would be brilliant to have something simple like this to start with so I could build on it to produce more sophisticated filters.
I feel sure one or two others might appreciate something basic as well.
Hoping someone will be able to help,
May thanks
4 Answers, 1 is accepted
The basic idea is the following:
You create a custom control that has UI. You do not need to follow a MVVM pattern. You can do everything with the good old event handlers and do not use any Binding's. That is up to you.
Responding to the user interacting with the UI you add or remove IFilterDescriptor instances to RadGridView's FilterDescriptors collection. Adding such a filter, filters the grid and removing it -- unfilters it.
There are two classes that you can use that implement IFilterDescriptor. For a single filter use the FilterDescriptor class. It is used to say something like (Name StartsWith "John") or (Country Contains "Land").
If you want to combine more than one filter logically into one, you should use the CompositeFilterDescriptor class which can combine more than one filter with a logical AND / OR. You can nest such descriptors in any tree-like manner.
So in your case you can create one CompositeFilterDescriptor and add three child descriptors -- one for Contains, one for StartsWith, and one for EndsWith. Since I am not sure what exactly you are trying to achieve, your operator will be either AND or OR depending on how you want to filter.
Then when the user clicks a filter button you will be adding this CompositeFilterDescriptor to RadGridView's FilterDescriptors collection. When the user clicks a clear button you will simply remove it from there. Something like this.
I have almost forgot about this and this is important.
Recently, I have explained this entire process in my step-by-step tutorial Custom Filtering with RadGridView for Silverlight. You should definitely study in great detail, in case you have not already done so. I am sure that after reading it a lot of things will begin to makes sense. The blog post has the full source code attached and runnable, explanations about everything, and nice diagrams and pictures. I am sure that it will get you going in no time.
Please, let us know if you have any specific questions regarding custom filtering and we will be glad to help you out.
Best wishes,
Ross
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
I guess I will have to think of something easier because without a simple working example I am obviously not going to get anywhere
Thanks anyway Ross.
Unless you provide us with something more specific, it will be hard for us to help you, since we really do not know what exactly are you tying to do.
Please, tell us what would you you like to do and how my advice in my previous post (not the blog, here in the support ticket) does not fit in your particular scenario. I based my answer on my guesses about what you are trying to achieve, but I might have guessed wrong. After all, you might be trying to achieve something that is not possible at all. That is why we really need to know what is your concrete goal. Once we know it we would be able to respond more productively.
Any specific questions and details about your project will be greatly appreciated. Also, if you have a sample dummy project with your custom filtering control which is under construction you can send it to us so we can take a look at it and see what is going so wrong.
We are looking forward to hearing from you.
Best wishes,
Ross
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Many thanks for your reply. You are obviously keen to help and for that, I am grateful.
Using the following site as an example, I wanted a simple Text Custom Filter for the Club column.
http://demos.telerik.com/silverlight/#GridView/CustomFilterControls
Your reply encouraged me to keep trying to work it out myself and I am glad to say that I think I have finally succeded.
As is often the case, it turned out to be a piece of cake once I removed all the clever bits from your examples.
A question of not being able to see the wood for the trees so to speak.
I tried to attach my program but you do not allow that apparently.
However, I include the following, in case someone finds it helpful.
Regards.
Pete
<
UserControl x:Class="SimpleFilter.CustomFilterControls.TextFilterControl"
xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100">
<
Border x:Name="LayoutRoot" Padding="0" BorderBrush="Black" BorderThickness="1" Background="White">
<Border Background="#FFDFE2E5" Margin="0" Padding="10" BorderBrush="#FF8A929E" BorderThickness="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="23"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
FontWeight="Bold"
FontSize="12"
Text="Filter Text" />
<ComboBox Grid.Row="1" x:Name="cboOperator" Width="100"/>
<TextBox Grid.Row="1" Grid.Column="1"
x:Name="txtOperand" Width="200" Margin="5,0,0,0" />
<Button Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Left"
Name="btnFilter"
Click="OnFilter"
Content="Filter"
Width="60" Margin="0,10,0,0"/>
<Button Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Right"
Name="btnClear"
Click="OnClear"
Content="Clear"
Width="60" Margin="0,10,0,0"/>
</Grid>
</Border>
</
Border>
</
UserControl>
using
System.Collections.Generic;
using
System.Windows;
using
System.Windows.Controls;
using
Telerik.Windows.Controls.GridView;
using
Telerik.Windows.Controls;
using
Telerik.Windows.Data;
namespace
SimpleFilter.CustomFilterControls
{
public partial class TextFilterControl : UserControl, IFilteringControl
{
private GridViewBoundColumnBase column;
private FilterDescriptor txtFilter;
public TextFilterControl()
{
InitializeComponent();
IEnumerable<FilterOperator> filterOperators = new List<FilterOperator>
{
FilterOperator.Contains,
FilterOperator.StartsWith,
FilterOperator.EndsWith
};
cboOperator.ItemsSource = filterOperators;
cboOperator.SelectedIndex = 0;
}
public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.Register(
"IsActive",
typeof(bool),
typeof(TextFilterControl),
new System.Windows.PropertyMetadata(false));
public bool IsActive
{
get { return (bool)GetValue(IsActiveProperty); }
set { SetValue(IsActiveProperty, value); }
}
public void Prepare(GridViewBoundColumnBase column)
{
this.column = column;
if (this.txtFilter == null)
this.txtFilter = new FilterDescriptor() { Member = column.DataMemberBinding.Path.Path };
}
private void OnFilter(object sender, RoutedEventArgs e)
{
this.txtFilter.Value = this.txtOperand.Text;
this.txtFilter.Operator = (FilterOperator)cboOperator.SelectedItem;
if (!this.column.DataControl.FilterDescriptors.Contains(this.txtFilter))
{
this.column.DataControl.FilterDescriptors.Add(this.txtFilter);
}
this.IsActive = true;
}
private void OnClear(object sender, RoutedEventArgs e)
{
this.column.DataControl.FilterDescriptors.Remove(this.txtFilter);
}
}
}