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"/>