Static Binding to Singleton Mysteriously Fails
I have the following singleton class which contains the following property:
public class Manager : INotifyPropertyChanged
{
public bool IsOnline
...
public st开发者_开发技巧atic Manager Instance
...
}
In the mark-up I am trying to change the color of a button based on this online property:
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<Binding Source="{x:Static storage:Manager.Instance}" Path="IsOnline"/>
</DataTrigger.Binding>
<Setter Property="Background" Value="#8000FF00"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
This binding <Binding Source="{x:Static storage:Manager.Instance}" Path="IsOnline"/>
fails with the exception:
Cannot convert the value in attribute 'Source' to object of type 'System.Windows.Markup.StaticExtension'.
I have quadruple-checked the "storage" namespace; I know it is both referenced and correct. The Instance property is static, so I do not understand why this binding would fail. I have similar bindings to static properties all over that work just fine.
Any ideas?
I've built a sample app that does exactly what you're describing, it works with no issues. You can download it here.
Have you tried using a ValueConverter to inspect the value that the StaticExtension is getting? (See method 2 on this page)
I just had exactly the same weird problem! Solution:
You MUST instantiate the class before doing any XAML operations with that class!
精彩评论