您可以创建附加的依赖属性,并将其应用于任何类型的控件。例如,对于TextBlock
. 下面是我的例子:
XAML
<Grid>
<TextBlock Name="SampleTextBlock" Width="200" Height="30"
Background="AntiqueWhite" Text="Sample TextBlock"
local:MyDependencyClass.MyPropertyForTextBlock="TestString" />
<StackPanel Width="100" Height="100" HorizontalAlignment="Left">
<Button Name="GetValueButton" Content="GetValueButton" Click="GetValue_Click" />
<Button Name="SetValueButton" Content="SetValueButton" Click="SetValue_Click" />
</StackPanel>
</Grid>
Code behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void GetValue_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(MyDependencyClass.GetMyPropertyForTextBlock(SampleTextBlock));
}
private void SetValue_Click(object sender, RoutedEventArgs e)
{
MyDependencyClass.SetMyPropertyForTextBlock(SampleTextBlock, "New Value");
MessageBox.Show(MyDependencyClass.GetMyPropertyForTextBlock(SampleTextBlock));
}
}
public class MyDependencyClass : DependencyObject
{
public static readonly DependencyProperty MyPropertyForTextBlockProperty;
public static void SetMyPropertyForTextBlock(DependencyObject DepObject, string value)
{
DepObject.SetValue(MyPropertyForTextBlockProperty, value);
}
public static string GetMyPropertyForTextBlock(DependencyObject DepObject)
{
return (string)DepObject.GetValue(MyPropertyForTextBlockProperty);
}
static MyDependencyClass()
{
PropertyMetadata MyPropertyMetadata = new PropertyMetadata(string.Empty);
MyPropertyForTextBlockProperty = DependencyProperty.RegisterAttached("MyPropertyForTextBlock",
typeof(string),
typeof(MyDependencyClass),
MyPropertyMetadata);
}
}
或者您可以使用属性Tag
,它只是为了存储附加信息而创建的。但有时,这个属性可能会被其他目标占据,或者可能因为他的名字而无法持有。使用直观的名称创建属性要好得多,例如:ValueForAnimation
、、、等。StringId
CanScrolling