2016-07-28 6 views
1

さまざまなタイプ(画像ソース、ジオメトリ、パス)のキャンバス画像が複数あり、文字列バインディングに応じて1だけ表示したいと考えています。キャンバスを動的に切り替えるwpf

これを行うにはどうすればよいですか?

私はこのコードをユーザーコントロールの中に置くことができるので、アプリの周りにこれらのイメージの多くがあり、どの1が表示されるかを選択することができます。

ので、同様:

<canvas> 
    <Data ="m24,98,07"> 
</canvas> 

犬:

<CanvasImage Image="Pie"/> 
<CanvasImage Image="Dog"/> 

が、それはあまりにも計算コストがそれら

パイキャンバス例ユーザーコントロールのビューおよび使用の可視性バインディングで宣言されたすべてを持っているだろうキャンバスの例:

<canvas> 
    <image source=""> 
<canvas> 
+0

私は今、この3回この質問を読んで、まだあなたが求めているものの見当もつかないきたいくつかのサンプルコード – suulisin

+0

を表示することができますしてください。再利用性についてですか?パフォーマンス?何かを束縛する方法? – lokusking

+0

基本的に私は今CanvasImageと呼ばれるユーザーコントロールを持っています。その中には、それぞれキャンバスの中にたくさんのイメージがあります。 私はそれぞれのキャンバスの可視性をバインドし、必要な場合を除いてすべてオフにします。私はこのユーザーコントロールを何度も何度も何度もオンにするだけで何度も再利用します。フォントの仕組みとほとんど同じです これを行うには良い方法がありますか? – Sam

答えて

0

このコンバータは、受け取った値に応じてイメージソースを直接返します。

namespace TestTreeView.Views 
{ 
    public class StringToImageConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      string file = ""; 

      string v = value as string; 
      switch (v) 
      { 
       case "Pie": 
        file = @".\path\to\your\pie.jpg"; 
        break; 
       case "Dog": 
        file = @".\path\to\your\dog.jpg"; 
        break; 
       default: 
        return null; 
      } 

      return new BitmapImage(new Uri(file, UriKind.RelativeOrAbsolute)); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

XAMLでの使用:

<Window xmlns:local="clr-namespace:YourNamespace.Views" ...> 
    <Window.Resources> 
     <local:StringToImageConverter x:Key="stringToImageConverter"/> 
    </Window.Resources> 

    <Grid> 
     <Canvas> 
      <Image Source="{Binding YourString, Converter={StaticResource stringToImageConverter}}"/> 
     </Canvas> 
    </Grid> 
</Window> 

オリジナル答え

私はあなたがコンバータを使用する必要があると思います。
バインドされた値がどのような値であるかを示すString型のConverterParameterを取得し、Canvasを表示するかどうかを示すVisiblityを返します。 XAMLで

namespace YourNamespace.Views 
{ 
    public class StringToCanvasVisibilityConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      string v = value as string; 
      string p = parameter as string; 
      return (v != null && p != null && v == p) ? Visibility.Visible : Visibility.Collapsed; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

使用法:

<Window xmlns:local="clr-namespace:YourNamespace.Views" ...> 
    <Window.Resources> 
     <local:StringToVisibilityConverter x:Key="stringToVisibilityConverter"/> 
    </Window.Resources> 

    <Grid> 
     <Canvas Visibility="{Binding YourString, Converter={StaticResource stringToVisibilityConverter}, ConverterParameter=Pie}"/> 
     <Canvas Visibility="{Binding YourString, Converter={StaticResource stringToVisibilityConverter}, ConverterParameter=Dog}"/> 
    </Grid> 
</Window> 
関連する問題