简体   繁体   中英

How do you reference a class through xaml?

I have a Xaml file that does not have any code behind. I would like to have it reference another class in the same project for a boolean. How do I reference another classes objects (note those objects are static).

Thank you in advance.

use the x:Static markup extension , for example (taken from MSDN):

<object property="{x:Static prefix:typeName.staticMemberName}" .../>

You'll need to adjust this to suit your example (or include more information in the question). Also note the following warning from the same MSDN page :

The default XAML namespace you use for WPF programming does not contain many useful static properties, and most of the useful static properties have support such as type converters that facilitate the usage without requiring {x:Static} . For static properties, you must map a prefix for a XAML namespace if one of the following is true:

  • You are referencing a type that exists in WPF but is not part of the default XAML namespace for WPF. This is a fairly common scenario for using x:Static . For example, you might use an x:Static reference with a XAML namespace mapping to the System CLR namespace and mscorlib assembly in order to reference the static properties of the Environment class.
  • You are referencing a type from a custom assembly.
  • You are referencing a type that exists in a WPF assembly, but that type is within a CLR namespace that was not mapped to be part of the WPF default XAML namespace. The mapping of CLR namespaces into the default XAML namespace for WPF is performed by definitions in the various WPF assemblies. Non-mapped CLR namespaces can exist if that CLR namespace is composed mostly of class definitions that are not typically intended for XAML, such as System.Windows.Threading .

So, you might need to define a namespace such as

xmlns:prefix="clr-namespace:YOURNAMESPACE;assembly=YOURLIB"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM