简体   繁体   中英

WPF control local:InverseBooleanConverter value is not a valid MarkupExtension expression

I am getting this error: local:InverseBooleanConverter value is not a valid MarkupExtension expression. Cannot resolve InverseBooleanConverter

So My I my class InverseBooleanConverter is in the same namespace at my control and I do have this:

xmlns:local="clr-namespace:SpriteControl"

I looked up what a MarkupExtension expression and I saw a few of them, but I do not know with one I should use.

I tried x:Reference but is did not work here is my code:

  <Button Command="{Binding SP1Btn}" Content="Apply New Value" FontSize="12" IsEnabled="{Binding Path=IsReadOnly,ElementName=OPTextBox, Converter={Reference: InverseBooleanConverter}}" />

I also did try using local but that did not work.

I tried building the solution without any errors and then try the line and that did not work.

namespace SpriteControl
{

public class InverseBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            bool testValue = (bool)value;
            return !testValue; // or do whatever you need with this boolean
        }
        catch { return true; } // or false
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Convert(value, targetType, parameter, culture);
    }
}

}

In order to get your Converter to work you need to declare it in your xaml, like so:

<UserControl or Window ...
    xmlns:local="clr-namespace:SpriteControl">
    <UserControl.Resources>
        <local:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
    <UserControl.Resources/>
<Button Command="{Binding SP1Btn}" Content="Apply New Value" FontSize="12" IsEnabled="{Binding Path=IsReadOnly,ElementName=OPTextBox, Converter={StaticResource InverseBooleanConverter}}" />  

Your command should implement ICommand interface and it should have can execute associated with it. This will handle IsEnabled property on the button for you.

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