0

I am currently scratching my head as to why the visibility of certain canvases are not being updated by the binding I have in place. This is the environment...

I have the following class that implements INotifyPropertyChanged.

public partial class PrepareSystem : FlowWindow ,INotifyPropertyChanged
{
    //...

    private prepateTreatmentState _prepareState;
    prepateTreatmentState PrepareState
    {
        get { return _prepareState; }
        set 
        { 
            if(value != _prepareState)
            {
                _prepareState = value;
                RaisePropertyChanged("PrepareState");
            }
        }
    }

//...

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string prop)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(prop));
        }
    }

//...

}

I then have several canvas visibilities bound to this propery

<Canvas x:Name="automaticAlignmentGVLarge" Canvas.Left="66" Canvas.Top="118" 
        Visibility="{Binding PrepareState, Converter={StaticResource enumConverter}, ConverterParameter='AUTOMATIC_ALIGNMENT_NON_TOPOPGRAPHY', FallbackValue=Hidden}">
    <Image Source="Resources/Elements/Images/327.png"/>
    <TextBlock Style="{StaticResource LargeInstructionText}" Text="{x:Static res:Strings.AUTOMATIC_ALIGNMENT}" Canvas.Top="17" Canvas.Left="35" Width="321" TextAlignment="Center" />
</Canvas> 

Where enumConverter is defined as the following...

public class EnumToVisibilityConverter : IValueConverter
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || parameter == null || !(value is Enum))
            return Visibility.Collapsed;

        var currentState = value.ToString();
        var stateStrings = parameter.ToString();
        var found = false;

        foreach (var state in stateStrings.Split(','))
        {
            found = (currentState == state.Trim());

            if (found)
                break;
        }

        return found ? Visibility.Visible : Visibility.Hidden;
    }

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

Essentially, I am trying to set the visibility of my canvas' when the active state of the system is in one or many enumerated states. However, whenever I reference PrepareState in my binding, I get a " WPF Error 40 BindingExpression path error: property not found on 'object'" error. The DataContext has been set in the FlowWindow_Loaded routine to this.DataContext = this which I thought would do the trick for me. Any ideas as to why the XAML is unable to detect the PrepareState propery within the PrepareSystem class?

Thank you very much in advance, Mike

UPDATE:

Leave it to me to miss the simple stuff. Thanks, that did the trick with respect to single parameter binding. However I am still having a problem with multiple converter parameters. Ie, I would like a canvas to be visible is my property it set to one of the following enum values...

COARSE_ALIGNMENT_TOPOGRAPHY

GENTIAN_VIOLET_ALIGNMENT

AUTOMATIC_ALIGNMENT_TOPOPGRAPHY

AUTOMATIC_ALIGNMENT_NON_TOPOPGRAPHY

The control is written as follows, with the converted used above. All controls that use a single converterparameter work, but this one does not.

<Button x:Name="autoAlign" 
        Canvas.Left="305" 
        Canvas.Top="639" 
        Content="{x:Static res:Strings.AUTO_ALIGN}"
        Style="{StaticResource LargeButtonWithText}" 
        Visibility="{Binding PrepareState, Converter={StaticResource enumConverter}, ConverterParameter='COARSE_ALIGNMENT_TOPOGRAPHY,GENTIAN_VIOLET_ALIGNMENT,AUTOMATIC_ALIGNMENT_TOPOPGRAPHY,AUTOMATIC_ALIGNMENT_NON_TOPOPGRAPHY'}"
        Click="AutoAlign_Click"/>
2
  • The answer is simple. Just make your 'prepateTreatmentState' property as public. Secondly, when you are working on WPF, try using some tools like Snoop while would show you the binding errors and binding values at run-time. Commented Aug 21, 2014 at 14:32
  • Of course I would miss the little things. Thanks for the response. I have updated the initial question with a subsequent question, thanks! Commented Aug 21, 2014 at 19:30

1 Answer 1

1

The binding Source Property must be public. Add public accessSpecifier to PrepareState property like

public prepateTreatmentState PrepareState
{
  get { return _prepareState; }
    set { 
        if(value != _prepareState)
        {
            _prepareState = value;
            RaisePropertyChanged("PrepareState");
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.