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