Can I set a DataContext to a static class?

前端 未结 1 747
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 04:23

I\'ve a static class which reads information from the application assembly.

I\'ve declared it static since the class needs no instance declaration and will only ever

相关标签:
1条回答
  • 2021-01-05 05:13

    You can bind to a static field or property by using the {x:Static} binding syntax.

    x:Static is used to get static fields and properties. You can set the datacontext to a static field, or property, but not to a static type.

    Example below:

    <DataContext Source="{x:Static prefix:typeName.staticMemberName}" />
    

    It'd be more appropriate for you to ignore the datacontext, and just use a {x:Static binding for each value you wish to bind. For example, below would bind the program name static property:

    <TextBlock Text="{Binding Source={x:Static pi:ProgramInfo.ProgramName}}" /> 
    
    | improve this answer | |
    0 讨论(0)
提交回复
热议问题