How to set filter control Filter1.Operator to Contains in xaml

6 posts, 1 answers
  1. Wayne
    Wayne avatar
    4 posts
    Member since:
    Apr 2012

    Posted 15 Apr 2014 Link to this post

    I currently have a custom filter control defined for a column in a RadGridView and I'm trying to set Filter1 and Filter2 to default to "Contains" and the Logical Operator to "Or" in the xaml. However I'm having a hard time figuring it out. The code sample I'm working with is below. Can someone help me figure out how to change Fitler1, Filter2 and the logical operator default value?

    <Telerik_Windows_Controls_GridView:FilterOperatorConverter x:Key="FilterOperatorConverter"/>
        <DataTemplate x:Key="ActionTemplate">
            <TextBlock Text="{Binding Converter={StaticResource FilterOperatorConverter}}"/>
        </DataTemplate>
        <Telerik_Windows_Controls_GridView:FilterCompositionLogicalOperatorConverter x:Key="FilterCompositionLogicalOperatorConverter"/>
        <DataTemplate x:Key="LogicalOperatorTemplate">
            <TextBlock Text="{Binding Converter={StaticResource FilterCompositionLogicalOperatorConverter}}"/>
        </DataTemplate>
        <ControlTemplate x:Key="FilteringControlTemplate" TargetType="telerik:FilteringControl">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Margin="{TemplateBinding Margin}">
                <Border BorderBrush="{StaticResource ControlInnerBorder}" BorderThickness="1" Background="{TemplateBinding Background}">
                    <StackPanel HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MaxWidth="350" Margin="{TemplateBinding Padding}" MinWidth="200" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                        <StackPanel x:Name="PART_DistinctFilter" Visibility="{TemplateBinding DistinctFiltersVisibility}">
                            <Grid>
                                <telerik:RadButton x:Name="PART_FilterCloseButton" Grid.Column="1" HorizontalAlignment="Right" Height="13" Margin="0" telerik:StyleManager.Theme="{StaticResource Theme}" VerticalAlignment="Top" Width="13">
                                    <Path Data="M4,4L5,4 5,5 4,5z M0,4L1,4 1,5 0,5z M3,3L4,3 4,4 3,4z M1,3L2,3 2,4 1,4z M2,2L3,2 3,3 2,3z M4,0L5,0 5,1 4,1 4,2 3,2 3,0.99999994 4,0.99999994z M0,0L1,0 1,0.99999994 2,0.99999994 2,2 1,2 1,1 0,1z" Fill="{StaticResource GridView_IndicatorPrimaryColor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Height="6" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="6"/>
                                </telerik:RadButton>
                            </Grid>
                        </StackPanel>
                        <StackPanel Margin="0,2" Visibility="{TemplateBinding FieldFiltersVisibility}">
                            <TextBlock Margin="0,2,0,0" telerik:LocalizationManager.ResourceKey="GridViewFilterShowRowsWithValueThat"/>
                            <telerik:RadComboBox x:Name="PART_Filter1ComboBox" ItemTemplate="{StaticResource ActionTemplate}" ItemsSource="{Binding AvailableActions}" Margin="0,2,0,2" SelectedItem="{Binding Filter1.Operator, Mode=TwoWay}" telerik:StyleManager.Theme="{StaticResource Theme}"/>
                            <ContentControl x:Name="PART_Filter1ContentControl" DataContext="{Binding Filter1}" HorizontalContentAlignment="Stretch" Margin="0, 2" VerticalContentAlignment="Stretch" />
                            <telerik:RadComboBox x:Name="PART_LogicalOperatorsComboBox" ItemTemplate="{StaticResource LogicalOperatorTemplate}" ItemsSource="{Binding LogicalOperators}" Margin="0,2" SelectedItem="{Binding FieldFilterLogicalOperator, Mode=TwoWay}" telerik:StyleManager.Theme="{StaticResource Theme}" />
                            <telerik:RadComboBox x:Name="PART_Filter2ComboBox" ItemTemplate="{StaticResource ActionTemplate}" ItemsSource="{Binding AvailableActions}" Margin="0,2" SelectedItem="{Binding Filter2.Operator, Mode=TwoWay}" telerik:StyleManager.Theme="{StaticResource Theme}"/>
                            <ContentControl x:Name="PART_Filter2ContentControl" DataContext="{Binding Filter2}" HorizontalContentAlignment="Stretch" Margin="0, 2" VerticalContentAlignment="Stretch"/>
                        </StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Button x:Name="PART_ApplyFilterButton" Grid.Column="0" Height="22" Margin="0,2,2,2" telerik:LocalizationManager.ResourceKey="GridViewFilter" telerik:StyleManager.Theme="{StaticResource Theme}"/>
                            <Button x:Name="PART_ClearFilterButton" Grid.Column="1" Height="22" Margin="2,2,0,2" telerik:LocalizationManager.ResourceKey="GridViewClearFilter" telerik:StyleManager.Theme="{StaticResource Theme}"/>
                        </Grid>
                    </StackPanel>
                </Border>
            </Border>
        </ControlTemplate>
        <SolidColorBrush x:Key="GridView_FilteringControlBackground" Color="#FFE4E4E4"/>
        <SolidColorBrush x:Key="GridView_FilteringControlOuterBorder" Color="#FF848484"/>
        <SolidColorBrush x:Key="ControlForeground" Color="Black"/>
        <Style x:Key="LocomotiveFilteringControlStyle" TargetType="telerik:FilteringControl">
            <Setter Property="Template" Value="{StaticResource FilteringControlTemplate}"/>
            <Setter Property="Background" Value="{StaticResource GridView_FilteringControlBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource GridView_FilteringControlOuterBorder}"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="Margin" Value="0,2,0,0"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Foreground" Value="{StaticResource ControlForeground}"/>
        </Style>
  2. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 16 Apr 2014 Link to this post

    Hello,

    You cannot hard-code those settings for the FilteringControl in xaml.

    As to changing the default filter operators for Filter1 and Filter2, you can check the following help article: Change the Default Selected Filter Operators.

    As to assigning an Or LogicalOperator between the filters, you can change it per column's level.
    For example:
    public MainWindow()
    {
        InitializeComponent();
     
        var columnFilter = grid.Columns["Name"].ColumnFilterDescriptor as IColumnFilterDescriptor;
        columnFilter.FieldFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
    }

    I hope this helps.

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Wayne
    Wayne avatar
    4 posts
    Member since:
    Apr 2012

    Posted 16 Apr 2014 in reply to Dimitrina Link to this post

    Didie,
     Thanks the link to the "Change the Default Selected Filter Operators" worked perfectly. However the setting of the LogicalOperator was a little more tricky. I ended up putting the code after the data had been retrieved. For some reason it was getting overwritten if I set it before the data was retrieved.
     
     Also I tried to set the LogicalOperator in the FilterOperatorsLoading method and I could see the change, but it didn’t get reflected in the filter when it was opened. I would make sense to add to the FilterOperatorsLoading method to allow the other properties within the filter to be set to a default also.
     
        private void dg_DPDataGrid_FilterOperatorsLoading(object sender, FilterOperatorsLoadingEventArgs e)
        {
               
            if (e.Column.Header.ToString() == "Locomotive")
            {
                e.DefaultOperator1 = FilterOperator.Contains;
                e.DefaultOperator2 = FilterOperator.Contains;
     
                // Below line did not work
                e.Column.ColumnFilterDescriptor.FieldFilter.LogicalOperator = Telerik.Windows.Data.FilterCompositionLogicalOperator.Or;
            }                       
        }
     
     
    Thanks,
        Wayne
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 Apr 2014 Link to this post

    Hi Wayne,

    Indeed, the LogicalOperator cannot be changed when the DistinctValuesLoading event is raised. It is already set by this time.
    Thank you for your feedback. 

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Wayne
    Wayne avatar
    4 posts
    Member since:
    Apr 2012

    Posted 21 Apr 2014 in reply to Dimitrina Link to this post

    Didie,
    The DefaultOperators being set to the FilterOperator 'Contains' is working great, but I have an issue when the Clear Filter button is clicked. The FilterOperator is set back to 'Is Equals' after clearing the filter. I've been going through the online documentation this morning, but found nothing about this yet. Is there a way to keep the FilterOperators set to 'Contains' after the filter is cleared?

    Note: If the filter is closed and reopened, then the FilterOperators are set back to 'Contains' as expected.

    Thanks,
       Wayne
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Apr 2014 Link to this post

    Hello Wayne,

    Your observations are right, pressing the Clear Filter button will clear all the defined filters including the FilterOperators. This is the behavior by design and it cannot be configured otherwise.

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top