可见性绑定未更新。 WPF错误40 BindingExpression路径错误:'对象'上找不到属性

时间:2014-08-21 14:16:35

标签: c# wpf xaml data-binding datacontext

我目前正在摸索为什么某些画布的可见性没有被我所拥有的约束更新。这就是环境......

我有以下实现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));
        }
    }

//...

}

然后,我有几个画布可见性绑定到这个属性

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

将enumConverter定义为以下内容......

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();
    }
}

基本上,我试图设置我的画布的可见性&#39;当系统的活动状态处于一个或多个枚举状态时。但是,每当我在绑定中引用PrepareState时,我都会得到一个&#34; WPF错误40 BindingExpression路径错误:&#39; object&#39;&#34;错误。 DataContext已经在FlowWindow_Loaded例程中设置为this.DataContext = this,我认为这对我来说很有用。有关为什么XAML无法在PrepareSystem类中检测PrepareState属性的任何想法?

非常感谢您提前, 麦克

更新

让我想念简单的东西。谢谢,这就完成了单参数绑定的技巧。但是我仍然遇到多个转换器参数的问题。也就是说,我想要一个可见的画布是我的属性,它设置为以下枚举值之一......

COARSE_ALIGNMENT_TOPOGRAPHY

GENTIAN_VIOLET_ALIGNMENT

AUTOMATIC_ALIGNMENT_TOPOPGRAPHY

AUTOMATIC_ALIGNMENT_NON_TOPOPGRAPHY

控件编写如下,上面使用了转换后的控件。使用单个转换器参数的所有控件都可以工作,但是这个控制器没有。

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

1 个答案:

答案 0 :(得分:1)

绑定源属性必须是公共的。将公共accessSpecifier添加到PrepareState属性,如

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