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

Why should I get "BoolToRowHeightConverter is not supported in Project Foundation Foundation (WPF) project in xaml? I used a converter to convert rowheight to * and Auto in a grid based on the Expander 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(); } } 
+1
source share
2 answers

Typically, IValueConverter used as follows:

a) Add a namespace on the 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 to the Resources section of your page (or App.xaml :

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

c) Access the converter instance using the x:Key value you gave it:

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

You decide to reference the value converter using the x:Static markup extension ( {x:Static BoolToRowHeightConverter.Instance} ), but then you also need to specify the actual field or property that you are referring to ( Instance ). To do this, you need to add it to the BoolToRowHeightConverter class:

 public class BoolToRowHeightConverter : IValueConverter { // Convert and ConvertBack methods ... public static readonly BoolToRowHeightConverter Instance = new BoolToRowHeightConverter(); } 
0
source

Source: https://habr.com/ru/post/1238507/


All Articles