2012-02-27 9 views
0

特定の状況に応じて表示または非表示にするいくつかのキャンバスを持つUIElementを取得しました。私はこれがデザイナーとプログラムの実行中に見られるようにしたい。私はいくつかのBindingsとBooleanToVisibilityConverterを試しました。しかし、私は立ち往生し、私のエラーを見つけることができません。だからここのコードです:バインディングによる要素の可視性の設定

のUIElement(2つだけキャンバスとは、私は部分クラスでの応答特性を持っ)ParentWindowで

<component:AbstractComponent x:Class="View.LineComponent" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:component="clr-namespace:View" 
     x:Name="userControl" Width="70" Height="10" 
> 
    <component:AbstractComponent.Resources> 
     <BooleanToVisibilityConverter x:Key="VisibilityConverter" /> 
    </component:AbstractComponent.Resources> 
    <Canvas Width="{Binding ElementName=userControl, Path=ActualWidth}"> 
     <Canvas Name="Line" 
      Height="{Binding ElementName=userControl, Path=ActualHeight}" 
      Width="{Binding ElementName=userControl, Path=ActualWidth}" 
      Visibility="{Binding LineVisible, Converter={StaticResource VisibilityConverter}, FallbackValue=Hidden}" 
      > 
      <!-- Lot of stuff; Not interesting for the question--> 
     </Canvas> 
     <Canvas Name="Arrow" 
      Height="{Binding ElementName=userControl, Path=ActualHeight, TargetNullValue=6.397, FallbackValue=6.397}" 
      Width="{Binding ElementName=userControl, Path=ActualWidth, TargetNullValue=16.688, FallbackValue=16.688}" 
      Visibility ="{Binding ArrowVisible, Converter={StaticResource VisibilityConverter}, FallbackValue=Hidden}" 
      > 
      <!-- Lot of stuff; Not interesting for the question--> 
     </Canvas> 
    </Canvas> 
</component:AbstractComponent> 

使用

<component:LineComponent Height="50" Canvas.Top="100" Width="50" Canvas.Left="50" LineVisible="True"/> 

<component:LineComponent Height="50" Canvas.Top="200" Width="50" Canvas.Left="50" ArrowVisible="True"/> 

私はことを期待します最初のケースではLine - キャンバスが表示され、もう一方ではArrow - キャンバスが表示されます。彼らはどちらもHiddenのままです。私はまた別のアプローチを試して、LineVisibleArrowVisibleのプロパティをC#コードで直接宣言しましたが、どちらもうまくいきませんでした。何か案は?

答えて

2

あなたのLineVisibleArrowVisibleバインディングにはソースセットがありませんので、DataContextをソースとして使用します。ルートCanvas要素のDataContextをユーザーコントロールに設定して、すべての繰り返しのElementNameバインディングを省略することができる場合は、described in this blog post I wroteというように自分で楽にします。

+0

素晴らしい!もし私があなたのブログ記事を数時間早く見つけたのであれば。これはまさに私がやりたいことです。しかし、1つの質問:私は 'string'としてすべてのプロパティを入力する必要がありますか?私は 'Visibility'を使って、使用された型がデフォルト値の型に適合しないという例外を与えました。 – Nessuno

+0

@Nessunoうれしかったよ! ... "すべてのプロパティを文字列として入力する必要がありますか?"いいえ、必要なタイプを使用してください。 Visibility型のプロパティを公開し、これをUIにバインドできます。 – ColinE

関連する問題