Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison with QuickConverter

Tags:

c#

wpf

xaml

I want to do something when Idea.Status=="Verified" but QuickConverter (1 - 2) doesn't allow me to use any of these:

Binding="{qc:Binding '$P==Verified',P={Binding Path=Idea.Status}}"
Binding="{qc:Binding '$P=="Verified"',P={Binding Path=Idea.Status}}"

'Verified' is an unexpected token. Expecting white space.

Failed to tokenize expression "$P=Verified". Did you forget a '$'?

How can I tell quickconverter and XAML that I want to compare against a string?

like image 664
The One Avatar asked Jul 22 '15 20:07

The One


3 Answers

QuickConverter uses single quote for string literals. However within the markup extension you need to escape the single quote, so you need to add \ before it.

So your binding should be

Binding="{qc:Binding '$P==\'Verified\'',P={Binding Path=Idea.Status}}"
like image 194
Tedy Pranolo Avatar answered Oct 31 '22 13:10

Tedy Pranolo


I did it this way. It works the same as the chosen answer, but the xaml parser is much happier and doesn't throw annoyng (fake) errors

Binding="{Path=Idea.Status, Converter={qc:QuickConverter '$P == \'Verified\''}}"
like image 34
Nikzeno Avatar answered Oct 31 '22 12:10

Nikzeno


The only way i can came up with is by using the qc:MultiBinding

<Grid>
    <Button Content="Hi There !"  VerticalAlignment=" Center" HorizontalAlignment="Center" IsEnabled="{qc:MultiBinding '$P0 == $P1', P0={Binding Status}, P1={Binding Verified}}"></Button>
</Grid>

Verified is defined as a property in the ViewModel/CodeBehind

public String Verified { get; set; }

here the full code behind

 public partial class MainWindow : Window,INotifyPropertyChanged
{
    public String Verified = "Verified";

    private String _status = "Verified";
    public String Status
    {
        get
        {
            return _status;
        }

        set
        {
            if (_status == value)
            {
                return;
            }

            _status = value;
            OnPropertyChanged();
        }
    }
    public MainWindow()
    {
        InitializeComponent();

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
like image 37
SamTh3D3v Avatar answered Oct 31 '22 12:10

SamTh3D3v