zoukankan      html  css  js  c++  java
  • Silverlight 4 Binding Cheatsheet [转]

    Path

    <TextBlock Text="{Binding Path=PropertyName}" />
    <TextBlock Text="{Binding PropertyName}" />

    Path : Name of the property on the object in the Datacontext of the page. The Path keyword is optional. The two lines above are functionally identical.

    <TextBlock Text="{Binding Path=Instance.PropertyName}" />

    If the object in the DataContext has is another object with properties then you can bind to it's properties by using a fullstop.


    ElementName

    <TextBox x:Name="SomeTextBox" />
    <TextBlock Text="{Binding ElementName=SomeTextBox, Path=Text}" />

    ElementName : Specifies another element in the view tree to bind to.
    Path : The property on the source element to bind from.


    Converter

    <UserControl.Resources>
    <BindingsProject:TextToColourConverter x:Key="TextToColourConverter" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
    <StackPanel>
    <TextBox x:Name="SomeTextBox" />
    <TextBlock Text="{Binding ElementName=SomeTextBox, Path=Text}" />
    <TextBlock Text="Test text"
    Foreground
    ="{Binding ElementName=SomeTextBox,Path=Text, Converter={StaticResource TextToColourConverter}}" />
    </StackPanel>
    </Grid>
    public class TextToColourConverter: IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return someConvertedValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return someConvertedBackValue;
    }
    }

    Converter : StaticResource declared in Resources. Converter is defined in codebehind and implements IValueConverter


    ConverterParameter

    <TextBlock Text="Test text" Foreground="{Binding ElementName=SomeTextBox,Path=Text, Converter={StaticResource TextToColourConverter}, ConverterParameter=true}" />
    <TextBlock Text="Test text" Foreground="{Binding ElementName=SomeTextBox,Path=Text, Converter={StaticResource TextToColourConverter}, ConverterParameter='Blue'}" />

    String parameters can be passed to the value converter. Unfortunately you can not bind to the ConverterParameter.


    String Format

    <TextBlock Text="{Binding Path=TimeWorked, StringFormat=hh\\:mm}" />
    <TextBlock Text="{Binding Path=TimeWorked, StringFormat='h\\:mm'}" />
    <TextBlock Text="{Binding Path=StartDate, StringFormat=D}" />
    <TextBlock Text="{Binding Path=StartDate, StringFormat='MMM dd, yyyy'}" />
    <TextBlock Text="{Binding Path=StartDate, StringFormat=MMM\ dd\,\ yyyy}" />
    <TextBlock Text="{Binding Path=StartDate, StringFormat=yyyy-MM-dd}" />

    Use apostrophes to enclose string formats with special characters, or use \ to escape the special character.

    Numeric String Formats


    Null Value

    <TextBlock Text="{Binding TotalAmount, TargetNullValue=0}" />

    If the value from the bound property is null then it will be replaced with the TargetNullValue.


    Indexed Bindings

    <StackPanel>
    <TextBlock Text="{Binding Path=SimpleProp1}" />
    <TextBox Text="{Binding Path=ComplexProp[0].SimpleSubProp, Mode=TwoWay}" Width="200" Height="60"/>
    <TextBox Text="{Binding Path=DictionaryCollection[Mineral], Mode=TwoWay}" />
    </StackPanel>

    You can bind to any collection that has an index (is IEnumerable). If the collection is a key/value collection such as Dictionary or Hash, then the item can be referenced by the item's key.




     

  • 相关阅读:
    干货分享:如何使用Kubernetes的Ingress API
    十年OpenStack Ussuri最新版发布 主要改进在可靠性、安全性和用例支持等方面
    如何更好地优化容器的创建?这些技巧你务必收藏
    Kubernetes是容器化微服务的圣杯么?
    微服务是否真的需要服务网格?
    ZOOM火速收购加密公司Kaybase 能否补齐安全短板?
    5个实例告诉您:如何实施成功的容器化多云策略
    新基建火了,开源云计算渠道能做什么?
    盘点6个Kubernetes监视工具
    掌握这10种方法帮你快速在Linux上分析二进制文件
  • 原文地址:https://www.cnblogs.com/ericfine/p/2277606.html
Copyright © 2011-2022 走看看