微软把DLL都开源了 今天看了一下 很多WPF实现内容都在里面
https://referencesource.microsoft.com/
说附加属性 附加属性 是全局属性
用来规范全局变量 等等~ 是也可以 给控件 单独增加属性功能
举栗子
给textbox 增加附加属性 并不会增加 到Textbox 类里
功能 按Tab的时候 会全选 textbox2的内容
增加了一个类 并使用 propa 定义了附加属性
-
public
class
TextChange
-
{
-
-
-
public static bool GetSelectOn(DependencyObject obj)
-
{
-
return (
bool)obj.GetValue(SelectOnProperty);
-
}
-
-
public static void SetSelectOn(DependencyObject obj, bool value)
-
{
-
obj.SetValue(SelectOnProperty,
value);
-
}
-
-
// Using a DependencyProperty as the backing store for SelectOn. This enables animation, styling, binding, etc...
-
public
static
readonly DependencyProperty SelectOnProperty =
-
DependencyProperty.RegisterAttached(
"SelectOn",
typeof(
bool),
typeof(TextChange),
new PropertyMetadata(
false, onNotify));
-
-
private static void onNotify(DependencyObject d, DependencyPropertyChangedEventArgs e)
-
{
-
object item = d;
-
if (item
is TextBox)
-
{
-
(item
as TextBox).GotFocus += (s, arges) =>
-
{
-
(item
as TextBox).SelectAll();
-
};
-
}
-
-
-
}
-
}
在textbox 界面 引用 xmlns:local="clr-namespace:WpfApplication21" 命名空间
<TextBox HorizontalAlignment="Left" Height="23" Margin="111,133,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" local:TextChange.SelectOn="True" />