【发布时间】:2021-11-08 08:08:16
【问题描述】:
我正在尝试基于 TextBox 创建一个 UserControl,当它失去焦点时,如果它与 Regex 不匹配,则 Text 将被擦除。
我的问题如下:我已经在我的 UserControl 中将 TextBox 的 Text 属性与名为 Text 的 DependencyProperty 绑定了,但是当我在 TextBox 中写入错误的 Text 然后使其失去焦点时,它不会做任何事情.
用户控件 XAML:
<Grid>
<TextBox VerticalContentAlignment="Center" Text="{Binding Text, UpdateSourceTrigger=LostFocus, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" FontFamily="{Binding FontFamily, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" FontSize="{Binding FontSize, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</Grid>
CS 后面的用户控制代码(带有 DependencyProperty):
// Text of the FormatBox
public static readonly DependencyProperty CS_EXAMPLETEXT_PROPERTY = DependencyProperty.Register(nameof(Text), typeof(String), typeof(FormatBox));
public string Text
{
get { return (string)GetValue(CS_EXAMPLETEXT_PROPERTY); }
set {
if (Regex.IsMatch(value ?? "", RegexString ?? "")) SetValue(CS_EXAMPLETEXT_PROPERTY, value);
else SetValue(CS_EXAMPLETEXT_PROPERTY, "");
}
}
MainWindows XAML:
<!-- Test FormatBox -->
<controls:FormatBox Grid.Row="3" FontFamily="Calibri" FontSize="16" RegexString="^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$" />
但是,如果我尝试对普通属性做同样的事情并实现 INotifyPropertyChanged,它就像一个魅力。
CS后面的UserControl代码(具有普通属性):
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private Dictionary<string, object> _propertyValues = new Dictionary<string, object>();
protected T GetProperty<T>([CallerMemberName] string propertyName = null)
{
if (_propertyValues.ContainsKey(propertyName)) return (T)_propertyValues[propertyName];
return default(T);
}
protected bool SetProperty<T>(T newValue, [CallerMemberName] string propertyName = null)
{
T current = GetProperty<T>(propertyName);
if (!EqualityComparer<T>.Default.Equals(current, newValue))
{
_propertyValues[propertyName] = newValue;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
return true;
}
return false;
}
#endregion
// Text of the FormatBox
public string Text
{
get { return GetProperty<string>(); }
set {
if (Regex.IsMatch(value ?? "", RegexString ?? "")) SetProperty<string>(value);
else SetProperty<string>("");
}
}
你能帮我让它与 DependencyProperty 一起工作吗?
【问题讨论】:
标签: c# wpf xaml data-binding dependency-properties