zoukankan      html  css  js  c++  java
  • WP7备注(32)(Relative Source|在自定义控件的属性中为子控件的属性赋值|INotifyPropertyChanged)

    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>
  • 相关阅读:
    Sql Sugar
    GoLang 环境部署
    Typora 自动添加序号
    C# 操作 Oracle批量执行Insert Blob
    C# 生成读取二维码
    Asp.net core 使用Serilog记录日志
    Asp.net Core 将日志输出到文件
    云原生领域的一些技术展望
    C# BeginInvoke用法记录
    C# 委托及线程
  • 原文地址:https://www.cnblogs.com/otomii/p/2035229.html
Copyright © 2011-2022 走看看