标签属性,用于存放任何东西,所有控件都有Tag属性
Tag可以用来存放存放图层uri访问地址等等
![](https://upload-images.jianshu.io/upload_images/9128511-809cd2669b883ae3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/9128511-36929b31c2111b11.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
控件模板的数据获取
XXX.Tag as YYY
举例:
<Button Content="点击我获得Tag属性的内容" Height="100" Width="150" Tag="这是Tag的内容" Click="ButtonBase_OnClick"></Button>
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
//这里可以看出Tag是object,可以存放任意数据类型
string tag = (sender as Button).Tag as string;
MessageBox.Show($"Tag的内容是"{tag}"");
}
示例代码
https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Tag