2012-03-25 4 views
0

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') 

は誰もが、この動作に遭遇したため、バインディングエラーを防止するための回避策を見つけましたか?

答えて

1

Microsoftと相談した後で、これはまだ実行可能ファイルには何の影響もないVisual Studio 2010(Bug Dev10 | 817794)の未修正バグです。

しかし、私はVS2010のこれらのエラーメッセージの多くに直面しているので、重要なエラーメッセージは出力ウィンドウで失われます。したがって、私は、回避策を見つけることを試みて、代わりにImageBrushのたVisualBrushを使用して解決策を考え出した:

<Window x:Class="CSVisualBrush.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Visual Brush Opacity Binding" Height="350" Width="525"> 
    <Grid> 
     <TextBox Name="txt" 
       Height="23" 
       HorizontalAlignment="Left" 
       Margin="175,47,0,0" 
       VerticalAlignment="Top" 
       Width="120"> 
      <TextBox.Style> 
       <Style TargetType="{x:Type TextBox}"> 
        <Setter Property="Background"> 
         <Setter.Value> 
          <VisualBrush Stretch="None" 
             AlignmentX="Right" 
             AlignmentY="Center" 
             > 
           <VisualBrush.Visual> 
            <Image Name="img" Source="/CSVisualBrush;component/search.png" /> 
           </VisualBrush.Visual> 
          </VisualBrush> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </TextBox.Style> 
     </TextBox> 
    </Grid> 
</Window> 

XAMLで定義されたときにバインディングが動作しませんので、私は背後にあるコードでそれを作成します。

public MainWindow() { 
    InitializeComponent(); 

    // Set binding to opacity of the image 
    // REMARK: Binding doesn't work within XAML 
    Image img = (Image)((VisualBrush)txt.Background).Visual; 
    Binding b = new Binding(); 
    b.Source = txt; 
    b.Path = new PropertyPath("Text"); 
    b.Converter = new EmptyStringToNotOpacityConverter(); 
    img.SetBinding(Image.OpacityProperty, b); 
} 

知るまで、私はその理由を知っていない理由をコードではなく、XAMLでバインディング作品。 誰かが同じ問題で動いたら、私のウェブサイトwww.logiclink.deに回避策VS2010ソリューションを入れました。

関連する問題