WPF usercontrol 自定义依赖属性
生活随笔
收集整理的這篇文章主要介紹了
WPF usercontrol 自定义依赖属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.依賴屬性不同意一般屬性,一般屬性主要定義在對象中,而依賴屬性是存在一個特殊的依賴屬性表中。
2.當我們觸發改變值時,需要通過SetValue這種方式進行觸發。
UserControl1.xaml:
<UserControl x:Class="WpfApplication1.UserControl1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApplication1"mc:Ignorable="d" Name="myUserControl"d:DesignHeight="300" d:DesignWidth="300"><Grid><TextBox Name="textBox" TextChanged="textBox_TextChanged"></TextBox></Grid> </UserControl>?
UserControl1.xml.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace WpfApplication1 {/// <summary>/// UserControl1.xaml 的交互邏輯/// </summary>public partial class UserControl1 : UserControl{public UserControl1(){InitializeComponent();}public static readonly DependencyProperty TextContentProperty;static UserControl1(){UserControl1.TextContentProperty =DependencyProperty.Register("TextContent",typeof(string), typeof(UserControl1));}public string TextContent{get{return (string)GetValue(UserControl1.TextContentProperty);}set{SetValue(UserControl1.TextContentProperty, value);}}private void textBox_TextChanged(object sender, TextChangedEventArgs e){TextBox box = (TextBox)sender;this.SetValue(TextContentProperty, box.Text);}} } View Code?
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApplication1"mc:Ignorable="d"Name="MainWindowName"Title="MainWindow" Height="350" Width="525"><Grid><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><TextBlock Name="tc" Grid.Row="0" Width="100" Height="30" Text="{Binding MyValue,ElementName=MainWindowName}"></TextBlock><local:UserControl1 x:Name="uc" Grid.Row="1" Width="50" Height="50" TextContent="{Binding MyValue,Mode=TwoWay,ElementName=MainWindowName}"></local:UserControl1></Grid> </Window>?
MainWindow.xaml.cs:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace WpfApplication1 {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window,INotifyPropertyChanged{public MainWindow(){InitializeComponent();this.DataContext = this;}private string myVar;public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string name){PropertyChangedEventHandler handler = PropertyChanged;if (handler != null){handler(this, new PropertyChangedEventArgs(name));}}public string MyValue{get { return myVar; }set { myVar = value;OnPropertyChanged("MyValue");}}} } View Code?
轉載于:https://www.cnblogs.com/xcong/p/6490402.html
總結
以上是生活随笔為你收集整理的WPF usercontrol 自定义依赖属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 认识多渲染目标(Multiple Ren
- 下一篇: 电子表格数据结构2