简体   繁体   中英

Why would I get a "BoolToRowHeightConverter is not supported in a Windows Presentation Foundation(WPF) project error in xaml?

Why would I get a "BoolToRowHeightConverter is not supported in a Windows Presentation Foundation(WPF) project error in xaml? I was using a converter to convert rowheight to * and Auto in a grid based on the expander's IsExpanded property.

Code in xaml:

     <RowDefinition Height="{Binding IsExpanded, ElementName=Expander5, Converter={x:Static BoolToRowHeightConverter.Instance}}"/>

Code in xaml.cs:

    public class BoolToRowHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object   parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value) return new GridLength(1, GridUnitType.Star);
            else
                return GridLength.Auto;

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


    }

Typically, IValueConverter s are used like this:

a) Add a namespace in your XAML page that references your converter class... usually it looks something like this:

xmlns:Converters="clr-namespace:WpfApplication1.Converters"

b) Add an instance of your converter class into the Resources section of your page (or of App.xaml :

<Window.Resources>
    <Converters:BoolToRowHeightConverter x:Key="BoolToRowHeightConverter" />
    ...
</Window.Resources>

c) Reference your converter instance by the x:Key value that you gave it:

<RowDefinition Height="{Binding IsExpanded, ElementName=Expander5, 
    Converter={StaticResource BoolToRowHeightConverter}}" />

You have decided to reference the value converter by using the x:Static markup extension ( {x:Static BoolToRowHeightConverter.Instance} ) but then you also need to provide the actual field or property that you reference ( Instance ). To do that you need to add it to the BoolToRowHeightConverter class:

public class BoolToRowHeightConverter : IValueConverter
{

  // Convert and ConvertBack methods ...

  public static readonly BoolToRowHeightConverter Instance = new BoolToRowHeightConverter();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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