Gatoninja
Gatoninja

Reputation: 63

InvalidCastException while binding with converter

I have a problem with a binding, I want to change an icon when the column has at least one element filtered. But it is not working.

This is my converter:

public class FilteredToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int index = ((DataGridColumnHeader)((MahApps.Metro.IconPacks.PackIconMaterial)value).TemplatedParent).Column.DisplayIndex;
        return !((AdvancedDataGrid)parameter).FilterLists[index].Any(item => item.NotFiltered);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

A filter for each column is generated with a contentTemplate, and I have 2 problems, If I set this style in controlTemplate.Resources the dataGrid reference is not found. If I set the style in the DataGrid.Resources then I get

InvalidCastException: Can not convert an object from type 'System.Windows.Controls.DataGrid' to type 'MahApps.Metro.IconPacks.PackIconMaterial'.

This is the style:

<Style TargetType="iconPacks:PackIconMaterial">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Converter={StaticResource FilteredToIcon}, ConverterParameter={x:Reference dataGrid}}" Value="True">
            <Setter Property="Kind" Value="FilterMenu"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

And this is a summary of the whole XAML of my custom AdvancedDataGrid:

<DataGrid x:Class="ZOT.GUI.Items.AdvancedDataGrid"
          x:Name="dataGrid" 
             xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             xmlns:local="clr-namespace:ZOT.GUI.Items">
    <DataGrid.Resources>
        <local:FilteredToIconConverter x:Key="FilteredToIcon" />
        <!-- The style can be here-->      
    </DataGrid.Resources>

    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <ControlTemplate.Resources>
                            <!-- The style can be here as well, whatever that works-->
                        </ControlTemplate.Resources>
                            <Grid>
                                <ContentPresenter/>
                                <ToggleButton x:Name="Y">
                                    <!-- This is the Icon I want to change -->
                                    <iconPacks:PackIconMaterial Kind="Filter"/>
                                </ToggleButton>
                                <Popup x:Name="pop" Width="auto" IsOpen="{Binding IsChecked, ElementName=Y,Mode=TwoWay}">
                                    <!-- .... -->
                                </Popup>
                            </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>

If you have any clue, tell me please. Thank you.

Upvotes: 1

Views: 159

Answers (1)

mm8
mm8

Reputation: 169400

If you want to bind to the PackIconMaterial itself, you should set the RelativeSource property of the binding to RelativeSource.Self:

<Style TargetType="iconPacks:PackIconMaterial">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Converter={StaticResource FilteredToIcon}, ConverterParameter={x:Reference dataGrid}, 
            RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Kind" Value="FilterMenu"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

That's the only way you will be able to cast value to MahApps.Metro.IconPacks.PackIconMaterial in the converter.

There are probably better ways of solving whatever you're trying to do though.

Upvotes: 1

Related Questions