This is a migrated thread and some comments may be shown as answers.

Bind FilterDescriptor Value to TreeView selected item

3 Answers 331 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 16 Mar 2012, 12:19 PM

Good morning,

I'm trying to bind a FilterDescriptor value to the tag value of a treeview item. Is this possible?

I have the following code but it doesn't work:

<telerikData:FilterDescriptor Member="ParkID" Operator="IsEqualTo" Value="{Binding ElementName=treeParks, Path=SelectedItem.Tag}" />

Thank you for your time,

Rob

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 16 Mar 2012, 01:06 PM
Hello Rob,

 Please check this forum thread for details on that matter.

Regards,
Didie
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Robert
Top achievements
Rank 1
answered on 16 Mar 2012, 01:09 PM
I found the following post which is along the lines of what I'm looking for (scroll to the bottom of the forum post for Silverlight Demo):
http://www.telerik.com/community/forums/silverlight/gridview/mvvm-search-as-you-type.aspx

I changed my project as per what I can see in the example.

Added this class to my project - CustomFilterDescriptor:
{
    public class CustomFilterDescriptor : FilterDescriptorBase
    {
        private RadGridView grid;
        private readonly CompositeFilterDescriptor compositeFilterDesriptor;
        private static readonly ConstantExpression TrueExpression = System.Linq.Expressions.Expression.Constant(true);
        private string filterValue;
 
        public CustomFilterDescriptor(RadGridView source)
        {
            this.compositeFilterDesriptor = new CompositeFilterDescriptor();
            this.compositeFilterDesriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
 
            this.grid = source;
        }
 
        public string FilterValue
        {
            get
            {
                return this.filterValue;
            }
            set
            {
                if (this.filterValue != value)
                {
                    this.filterValue = value;
                    this.UpdateCompositeFilterValues();
                    this.OnPropertyChanged("FilterValue");
                }
            }
        }
 
        protected override System.Linq.Expressions.Expression CreateFilterExpression(ParameterExpression parameterExpression)
        {
            if (string.IsNullOrEmpty(this.FilterValue))
            {
                return TrueExpression;
            }
            try
            {
                return this.compositeFilterDesriptor.CreateFilterExpression(parameterExpression);
            }
            catch
            {
            }
 
            return TrueExpression;
        }
 
        private IFilterDescriptor CreateFilterForColumn(GridViewDataColumn column)
        {
            FilterOperator filterOperator = GetFilterOperatorForType(column.DataType);
            FilterDescriptor descriptor = new FilterDescriptor(column.UniqueName, filterOperator, this.filterValue);
            descriptor.MemberType = column.DataType;
 
            return descriptor;
        }
 
        private static FilterOperator GetFilterOperatorForType(Type dataType)
        {
            return dataType == typeof(string) ? FilterOperator.Contains : FilterOperator.IsEqualTo;
        }
 
        private void UpdateCompositeFilterValues()
        {
            this.compositeFilterDesriptor.FilterDescriptors.Clear();
 
            foreach (GridViewDataColumn column in grid.Columns.OfType<GridViewDataColumn>())
            {
                this.compositeFilterDesriptor.FilterDescriptors.Add(this.CreateFilterForColumn(column));
            }
 
            foreach (FilterDescriptor descriptor in this.compositeFilterDesriptor.FilterDescriptors)
            {
                object convertedValue = DefaultValue(descriptor.MemberType);
 
                try
                {
                    convertedValue = Convert.ChangeType(this.FilterValue, descriptor.MemberType, CultureInfo.CurrentCulture);
                }
                catch
                {
                }
 
                if (descriptor.MemberType.IsAssignableFrom(typeof(DateTime)))
                {
                    DateTime date;
                    if (DateTime.TryParse(this.FilterValue, out date))
                    {
                        convertedValue = date;
                    }
 
                }
 
                descriptor.Value = convertedValue;
            }
        }
 
        private static object DefaultValue(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
 
            return null;
        }
    }
}



Altertered my TreeView markup to: *** some how I need to bind to the SelectedItem Tag property ***
<telerik:RadTreeView Name="treeParks" Grid.Row="1" Margin="8" IsRootLinesEnabled="False" IsLineEnabled="True" SelectedItem="{Binding Filter.FilterValue, Mode=TwoWay}">
            </telerik:RadTreeView>

My GridView markup is:
<telerik:RadGridView x:Name="gridAddresses" Grid.Column="2" IsFilteringAllowed="False">
 </telerik:RadGridView>

Added the following C# code/method:
CustomFilterDescriptor _Filter;
public CustomFilterDescriptor Filter
{
    get
    {
        if (_Filter == null)
        {
            _Filter = new CustomFilterDescriptor(gridAddresses);
            gridAddresses.FilterDescriptors.Add(_Filter);
        }
        return _Filter;
    }
}


Do you have any thoughts on what's going wrong?

Thank you again for your time and help.

0
Dimitrina
Telerik team
answered on 19 Mar 2012, 10:47 AM
Hi,

 What was the result after you tried the suggestion in the forum post (that I suggested you before)? I am a little bit confused from your second reply. Where do you use the code snippet from your first post:

<telerikData:FilterDescriptor Member="ParkID" Operator="IsEqualTo" Value="{Binding ElementName=treeParks, Path=SelectedItem.Tag}" />

All the best,
Didie
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Robert
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Robert
Top achievements
Rank 1
Share this question
or