How WPF XAML Binding Static Resources
PS: I haven't written WPF for a while, and some technologies have forgotten how to implement it. The egg hurts. Write it down when you encounter a technology in the future.
Method 1: Via {Binding MailPattern, Source={x:Static controls:RegexPatterns.Instance}}
1: First give an example of a resource class RegexPatterns.
namespace DN.Controls.Util
{
public sealed class RegexPatterns
{
private static RegexPatterns _instance;
public static RegexPatterns Instance
{
get
{
if (_instance == null)
_instance = new RegexPatterns();
return _instance;
}
protected set => _instance = value;
}
/// <summary>
/// 邮件正则匹配表达式
/// </summary>
public string MailPattern {
get; } =
@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
}
}
2: XAML use
<Grid x:Class="DN.ORM.Designer.ServerConfigForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://schemas.denisvuyka.wordpress.com/DN.Controls"
Margin="10">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/DN.Controls;component/Themes/Styles/TextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<controls:MSTextBox Width="300"
controls:InfoElement.Necessary="True"
controls:InfoElement.Placeholder=""
controls:InfoElement.RegexPattern="{Binding MailPattern , Source={x:Static controls:RegexPatterns.Instance}}"
controls:InfoElement.Title="服务器IP"
controls:InfoElement.TitlePlacement="Left"
controls:InfoElement.TitleWidth="120"
ShowClearButton="True" />
</Grid>
Observe the following code:
"{Binding MailPattern , Source={x:Static controls:RegexPatterns.Instance}}"
among them
x:Static : The referenced static property can be used to provide the value of the property in XAML.
controls : points to the domain name space of the RegexPatterns class.
[assembly: XmlnsDefinition(“http://schemas.denisvuyka.wordpress.com/DN.Controls”, “DN.Controls.Util”)]
RegexPatterns.Instance : The static object of RegexPatterns.