简体   繁体   English

与转换器绑定时出现 InvalidCastException

[英]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.每列的过滤器是用contentTemplate生成的,我有两个问题,如果我在controlTemplate.Resources设置了这个样式,则找不到 dataGrid 引用。 If I set the style in the DataGrid.Resources then I get如果我在DataGrid.Resources设置样式,那么我得到

InvalidCastException: Can not convert an object from type 'System.Windows.Controls.DataGrid' to type 'MahApps.Metro.IconPacks.PackIconMaterial' . 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 :这是我的自定义AdvancedDataGrid的整个 XAML 的摘要:

<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.谢谢你。

If you want to bind to the PackIconMaterial itself, you should set the RelativeSource property of the binding to RelativeSource.Self :如果您想绑定到PackIconMaterial本身,您应该将绑定的RelativeSource属性设置为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.这是您能够将值转换为转换器中的MahApps.Metro.IconPacks.PackIconMaterial的唯一方法。

There are probably better ways of solving whatever you're trying to do though.不过,可能有更好的方法来解决您想要做的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM