JMK
JMK

Reputation: 28059

Why isn't my type converter working

Tearing my hair out here! I have this type-converter:

class CouponBarcodeToVisibilityConverterColumn : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (DesignerProperties.IsInDesignMode)
        {
            if ((string)parameter == "123456")
            {
                return Visibility.Visible;
            }
            return Visibility.Hidden;
        }

        if (value == null)
        {
            return Visibility.Visible;
        }

        var barcodesWanted = ((string)parameter).Split(System.Convert.ToChar("_"));
        var actualBarcode = (string)value;

        return barcodesWanted.Any(barcodeWanted => barcodeWanted == actualBarcode) ? Visibility.Visible : Visibility.Hidden;
    }

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

I have a UserControl with the following Resources section:

<UserControl.Resources>
        <converters:CouponBarcodeToVisibilityConverterColumn x:Key="CouponBarcodeToVisibilityConverter1"/>
</UserControl.Resources>

I have a model called Bet, it looks like this:

public class Bet : INotifyPropertyChanged
{
    //Lots of other stuff

    private string _barcode;

    public string Barcode
    {
        get { return _barcode; }
        set
        {
            if (value == _barcode) return;
            _barcode = value;
            OnPropertyChanged("Barcode");
        }
    }

    //Lots of other stuff
}

In the ViewModel which is the DataContext of my user control I have an Observable Collection of Bet. Back to my user control, I have a stack panel, the data context of which is the aforementioned Observable Collection.

Inside the Stack Panel I have a DataGrid, the ItemsSource property is simply {Binding}, deferring the binding up the tree as it were.

Inside my DataGrid I have this column:

<DataGridCheckBoxColumn x:Name="IsEwColumn" Binding="{Binding Wagers[0].IsEw,UpdateSourceTrigger=PropertyChanged}" Header="Each Way" Visibility="{Binding Path=Barcode, Converter={StaticResource CouponBarcodeToVisibilityConverter1}, ConverterParameter=123456}" Width="Auto"/>

The other element of the binding works perfectly (the checkbox is ticked whenever it is supposed to be) but my type converter is not. The breakpoint doesn't even get hit. The Barcode property inside Bet is definitely equal to 123456.

What have I missed?

Upvotes: 0

Views: 285

Answers (1)

DermFrench
DermFrench

Reputation: 4057

What you have here is a list of bets for the items source of the data grid. If you think about it

Bet1 could evaluate to visible when passed via type converter. Bet2 could evaluate to visible when passed via type converter. Bet3 could evaluate to collapsed when passed via type converter.

How would the datacolumn be both visible and collapsed at the same time.

You can't bind to visibility like that, unless you had an overall variable on the list or something that it could bind to.

Upvotes: 1

Related Questions