Relative Source:
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=FontFamily}" />
在自定义控件的属性中为子控件的属性赋值:
public partial class BorderedText : UserControl { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BorderedText), new PropertyMetadata(null)); public string Text { set { SetValue(TextProperty, value); } get { return (string)GetValue(TextProperty); } } } <TextBlock Text="{Binding ElementName=this, Path=Text}"/>
用于自定义控件的子控件的属性开放,如上面那句代码,当BorderedText的Text属性赋予新值的时候,内部子控件TextBlock会自动绑定所赋的新值
INotifyPropertyChanged:
实现此接口的数据源类,也可以进行实时更新
public int Second { protected set { if (value != sec) { sec = value; OnPropertyChanged(new PropertyChangedEventArgs("Second")); } } get { return sec; } } protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { if (PropertyChanged != null) PropertyChanged(this, args); }
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel DataContext="{Binding Source={StaticResource clock}}" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Text="{Binding Path=Hour}" /> <TextBlock Text="{Binding Path=Minute, Converter={StaticResource stringFormat}, ConverterParameter=':{0:D2}'}" /> <TextBlock Text="{Binding Path=Second, Converter={StaticResource stringFormat}, ConverterParameter=':{0:D2}'}" /> </StackPanel> </Grid>