WPF 绑定到附加属性 绑定到只读属性


绑定有两个需要注意的地方

1.如果绑定到 附加属性(Binding Attached Property),需要加上括号,这个比较特别,例如

 

   <TextBox x:Name="tbUserName" 
               Width="200" 
               Grid.Column="0"
               Margin="10,10,0,0"         
               Foreground="Gray"
               nasSetting:TextBoxMaskHelper.MaskText="Please input username"
               Text="{Binding Path=(nasSetting:TextBoxMaskHelper.MaskText),Mode=OneWay,ElementName=tbUserName}"
               Height="30" CharacterCasing="Normal">
<TextBox.Foreground>
          <MultiBinding Converter="{StaticResource MaskBrushConverter}">
            <Binding Path="Text"   ElementName="tbUserName" UpdateSourceTrigger="PropertyChanged"/>
            <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)"  ElementName="tbUserName" />
          </MultiBinding>
        </TextBox.Foreground>

2.如果绑定到只读的属性(Binding to readonly property),例如IsFocused,需要加上 Mode = OneWay

<TextBox.Text>
          <MultiBinding Converter="{StaticResource MaskTextConverter}" Mode="OneWay">
            <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)"  ElementName="tbUserName" />
            <Binding Path="IsFocused"   ElementName="tbUserName"  Mode="OneWay"/>
          </MultiBinding>
        </TextBox.Text>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM