我可以将DataContext设置为静态类吗? [英] Can I set a DataContext to a static class?

查看:148
本文介绍了我可以将DataContext设置为静态类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类,可从应用程序集读取信息。

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 be read directly from, application-wide. I have a control with several labels that I would like to use to display some of this information.

如何设置控件DataContext等于类?

How can I go about setting the controls DataContext equal to the class?

代码:

/// <summary>
/// Class for Reading Program Information.
/// </summary>
public static class ProgramInfo {
    private static Assembly ProgramAssembly = Assembly.GetEntryAssembly( );

    /// <summary>
    /// Get Program Name
    /// </summary>
    public static string ProgramName {
        get { return ProgramInfo.ProgramAssembly.GetCustomAttribute<AssemblyProductAttribute>( ).Product; }
    }

    /// <summary>
    /// Get Program Build Date
    /// </summary>
    public static DateTime BuildDate {
        get { return File.GetLastWriteTime( ProgramInfo.ProgramAssembly.Location ); }
    }

    /// <summary>
    /// Get Program Version (Major.Minor)
    /// </summary>
    public static string Version {
        get {
            System.Version V = ProgramInfo.ProgramAssembly.GetName( ).Version;
            return V.Major.ToString( ) + '.' + V.Minor.ToString( );
        }
    }

    /// <summary>
    /// Get Program Build (Build.Revision)
    /// </summary>
    public static string Build {
        get {
            System.Version V = ProgramInfo.ProgramAssembly.GetName( ).Version;
            return V.Build.ToString( ) + '.' + V.Revision.ToString( );
        }
    }
}

XAML:

<UserControl x:Class="Tools.Controls.ProgramInformation"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:pi="clr-namespace:Tools.Controls">
<UserControl.DataContext>
    <pi:ProgramInfo/>
</UserControl.DataContext>
<!--Other Irrelevant Code-->
</UserControl>


推荐答案

您可以使用绑定到静态字段或属性 {x:Static} 绑定

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

x:Static用于获取静态字段和属性。您可以将数据上下文设置为静态字段或属性,但不能设置为静态类型。

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.

以下示例:

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

更适合您忽略数据上下文,而只需使用{x:Static绑定每个要绑定的值。例如,下面将绑定程序名称静态属性:

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

这篇关于我可以将DataContext设置为静态类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆