[英]Visibility binding not updating. WPF Error 40 BindingExpression path error: property not found on 'object'
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. 我有以下实现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... 其中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();
}
}
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.
但是,每当我在绑定中引用PrepareState时,都会出现“ WPF错误40 BindingExpression路径错误:在'object'上找不到属性”错误。 The DataContext has been set in the FlowWindow_Loaded routine to this.DataContext = this which I thought would do the trick for me.
DataContext已在FlowWindow_Loaded例程中设置为this.DataContext = this,我认为这可以帮到我。 Any ideas as to why the XAML is unable to detect the PrepareState propery within the PrepareSystem class?
关于XAML为什么无法在PrepareSystem类中检测PrepareState属性的任何想法?
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 COARSE_ALIGNMENT_TOPOGRAPHY
GENTIAN_VIOLET_ALIGNMENT GENTIAN_VIOLET_ALIGNMENT
AUTOMATIC_ALIGNMENT_TOPOPGRAPHY AUTOMATIC_ALIGNMENT_TOPOPGRAPHY
AUTOMATIC_ALIGNMENT_NON_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"/>
The binding Source Property must be public. 具有约束力的Source Property必须是公共的。 Add public accessSpecifier to PrepareState property like
将public accessSpecifier添加到PrepareState属性,例如
public prepateTreatmentState PrepareState
{
get { return _prepareState; }
set {
if(value != _prepareState)
{
_prepareState = value;
RaisePropertyChanged("PrepareState");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.