将扩展文本框中的依赖项 属性 绑定到 UserControl
Binding dependency property in extended textbox to UserControl
我已经扩展了文本框的功能,下面是 class 的一部分,附带 属性 RegularExpressionEnabled:
public class AlphaNumericTextBox : TextBox
{
#region DependencyProperties
public static readonly DependencyProperty RegularExpressionEnabledProperty =
DependencyProperty.Register("RegularExpressionEnabled", typeof(bool), typeof(AlphaNumericTextBox),
new UIPropertyMetadata(false));
public bool RegularExpressionEnabled
{
get { return (bool)GetValue(RegularExpressionEnabledProperty); }
set { SetValue(RegularExpressionEnabledProperty, value); Console.WriteLine("RegularExpressionEnabled:" + (bool)value); }
}
}
此 class 然后合并到 UserControl 中。
XAML:
<UserControl x:Class="Libs_ViewLevel.Controls.FilterItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Libs_ViewLevel"
xmlns:ctrls="clr-namespace:Libs_ViewLevel.Controls"
x:Name="UserControl">
<Grid x:Name="LayoutRoot" Height="Auto" >
<ctrls:AlphaNumericTextBox x:Name="AlphaNumericTbx"
Grid.ColumnSpan="2"
Text="{Binding AlphaNumericValue}"
RegularExpressionEnabled="{Binding RegExpressionEnabled}" />
</Grid>
</UserControl>
代码隐藏:
public partial class FilterItemControl : UserControl
{
public FilterItemControl()
{
InitializeComponent();
this.DataContext = this;
}
public bool RegExpressionEnabled
{
get { return (bool)GetValue(RegExpressionEnabledProperty); }
set { SetValue(RegExpressionEnabledProperty, value); Console.WriteLine("RegExpressionEnabled:" + (bool)value); }
}
public static readonly DependencyProperty RegExpressionEnabledProperty =
DependencyProperty.Register("RegExpressionEnabled", typeof(bool), typeof(FilterItemControl),
new UIPropertyMetadata(false));
我已将 UserControl 放在 Window 中,并在此 window 的代码隐藏中,放置了以下语句:txtbox.RegExpressionEnabled = true;
在 RegularExpressionEnabled 和 RegExpressionEnabled 中,您可以看到我放置了一个 Console.Write()。
RegExpressionEnabled 打印到“输出”屏幕,但没有到达第二个 RegularExpressionEnabled。
我在 AlphaNumericTextBox 上的绑定有问题 RegularExpressionEnabled="{Binding RegExpressionEnabled}"
,谁能指出正确的方向?
在 FilterItemControl 的隐藏代码中,RegExpressionEnabled 属性 应该是标准的 属性,因为这是您的新文本框所绑定的内容。您不需要在后面的 FilterItemControl 代码中依赖 属性,因为您已经在新文本框中拥有它。
现在不是设置 txtbox.RegExpressionEnabled = true(实际上应该是 txtbox.RegularExpressionEnabled = true),而是将 RegExpressionEnabled 属性 设置为 true,文本框将绑定到它。
您还需要在 RegExpressionEnabled 属性 的 setter 中引发 PropertyChanged 事件以触发数据绑定。
我已经扩展了文本框的功能,下面是 class 的一部分,附带 属性 RegularExpressionEnabled:
public class AlphaNumericTextBox : TextBox
{
#region DependencyProperties
public static readonly DependencyProperty RegularExpressionEnabledProperty =
DependencyProperty.Register("RegularExpressionEnabled", typeof(bool), typeof(AlphaNumericTextBox),
new UIPropertyMetadata(false));
public bool RegularExpressionEnabled
{
get { return (bool)GetValue(RegularExpressionEnabledProperty); }
set { SetValue(RegularExpressionEnabledProperty, value); Console.WriteLine("RegularExpressionEnabled:" + (bool)value); }
}
}
此 class 然后合并到 UserControl 中。
XAML:
<UserControl x:Class="Libs_ViewLevel.Controls.FilterItemControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Libs_ViewLevel"
xmlns:ctrls="clr-namespace:Libs_ViewLevel.Controls"
x:Name="UserControl">
<Grid x:Name="LayoutRoot" Height="Auto" >
<ctrls:AlphaNumericTextBox x:Name="AlphaNumericTbx"
Grid.ColumnSpan="2"
Text="{Binding AlphaNumericValue}"
RegularExpressionEnabled="{Binding RegExpressionEnabled}" />
</Grid>
</UserControl>
代码隐藏:
public partial class FilterItemControl : UserControl
{
public FilterItemControl()
{
InitializeComponent();
this.DataContext = this;
}
public bool RegExpressionEnabled
{
get { return (bool)GetValue(RegExpressionEnabledProperty); }
set { SetValue(RegExpressionEnabledProperty, value); Console.WriteLine("RegExpressionEnabled:" + (bool)value); }
}
public static readonly DependencyProperty RegExpressionEnabledProperty =
DependencyProperty.Register("RegExpressionEnabled", typeof(bool), typeof(FilterItemControl),
new UIPropertyMetadata(false));
我已将 UserControl 放在 Window 中,并在此 window 的代码隐藏中,放置了以下语句:txtbox.RegExpressionEnabled = true;
在 RegularExpressionEnabled 和 RegExpressionEnabled 中,您可以看到我放置了一个 Console.Write()。
RegExpressionEnabled 打印到“输出”屏幕,但没有到达第二个 RegularExpressionEnabled。
我在 AlphaNumericTextBox 上的绑定有问题 RegularExpressionEnabled="{Binding RegExpressionEnabled}"
,谁能指出正确的方向?
在 FilterItemControl 的隐藏代码中,RegExpressionEnabled 属性 应该是标准的 属性,因为这是您的新文本框所绑定的内容。您不需要在后面的 FilterItemControl 代码中依赖 属性,因为您已经在新文本框中拥有它。
现在不是设置 txtbox.RegExpressionEnabled = true(实际上应该是 txtbox.RegularExpressionEnabled = true),而是将 RegExpressionEnabled 属性 设置为 true,文本框将绑定到它。
您还需要在 RegExpressionEnabled 属性 的 setter 中引发 PropertyChanged 事件以触发数据绑定。