TextBoxの内容に応じて、画像をTextBoxの背景として表示したり非表示にしたりしたいとします。この目的のために、私はImageBrushを使用し、値変換でのTextBoxのTextプロパティにバインドして不透明度を調整する:ImageBrush不透明度がTextBoxにバインドする
<TextBox Height="23"
HorizontalAlignment="Left"
Margin="175,47,0,0"
VerticalAlignment="Top"
Width="120">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<!-- Converter -->
<local:EmptyStringToNotOpacityConverter x:Key="EmptyStringToNotOpacityConverter" />
</Style.Resources>
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="search.png"
Stretch="None"
AlignmentX="Right"
AlignmentY="Center"
Opacity="{Binding RelativeSource={RelativeSource AncestorType=TextBox},
Path=Text,
Converter={StaticResource EmptyStringToNotOpacityConverter}, Mode=OneWay}"
/>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
かなりまっすぐ進む値変換イストと〜1dの空の文字列を変換し、 TextBoxが空の場合はイメージを表示し、TextBoxが空でない場合はイメージを非表示にします。
すべてが予想されるように動作しますが、私は、起動時にバインディングエラーを取り除くことはできませんよ。
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TextBox', AncestorLevel='1''. BindingExpression:Path=Text; DataItem=null; target element is 'ImageBrush' (HashCode=41973697); target property is 'Opacity' (type 'Double')
は誰もが、この動作に遭遇したため、バインディングエラーを防止するための回避策を見つけましたか?