2017-02-20 5 views
1

とブール式への結合私は、彼らの値に基づいて、このような何かを画像を表示したい2つのブール値を持っています。WPF可視性は、複数の変数

+1

バインディングパスはこの種の式をサポートしていません。ビューモデル 'pu blicブールBoolean3 {get {return Boolean1 && Boolean2; }} 'そしてブール3にバインドする – ASh

+0

そのインラインのようなことをするためにB-e-t-t-e-r-簡単な方法が必要ですか? – Stacker

+0

マルチバインディングとマルチバリュー・フォンダーを使用する必要があります。http://www.codearsenal.net/2013/12/wpf-multibinding-example.html#.WKrPW_LNjMM – MikeT

答えて

7

は、XAMLで定義された&&演算子がありませんが、あなたはIMultiValueConverterいくつかのプロパティにバインドし、使用することができます

<Image> 
    <Image.Visibility> 
     <MultiBinding Converter="{StaticResource YourMultiConverter}"> 
      <Binding Path="Boolean1" /> 
      <Binding Path="Boolean2" /> 
     </MultiBinding> 
    </Image.Visibility> 
</Image> 

public class YourMultiValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool a = (bool)values[0]; 
     bool b = (bool)values[1]; 

     return a && b ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

それとも条件でImageスタイルを使用することができます

<Image> 
    <Image.Style> 
     <Style TargetType="Image"> 
      <Setter Property="Visibility" Value="Collapsed" /> 
      <Style.Triggers> 
       <MultiDataTrigger> 
        <MultiDataTrigger.Conditions> 
         <Condition Binding="{Binding Boolean1}" Value="True" /> 
         <Condition Binding="{Binding Boolean2}" Value="True" /> 
        </MultiDataTrigger.Conditions> 
        <Setter Property="Visibility" Value="Visible" /> 
       </MultiDataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Image.Style> 
</Image> 
+0

({StaticResource YourMultiConverter})を定義する方法を申し訳ありません。 – Stacker

+0

@Stackerコンバーターを定義するのと同じ方法で、関連するリソースディクショナリの静的リソースとして作成します。まだ持っていない場合は、ネームスペースを定義する必要があります。 – MikeT

+1

Window.Resources>。 – mm8

0

多値コンバータを使用すると、この問題を解決することができます

XAML

<TextBlock Name="textBlockOutput" Grid.Row="4" Grid.Column="2"> 
     <TextBlock.Text> 
      <MultiBinding Converter="{StaticResource MultiValueConverter}"> 
       <Binding Path="TextOne" /> 
       <Binding Path="TextTwo" /> 
       <Binding Path="TextThree" /> 
      </MultiBinding> 
     </TextBlock.Text> 
    </TextBlock> 




public class MultiValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     string one = values[0] as string; 
     string two = values[1] as string; 
     string three = values[2] as string; 
     if(!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(two) && !string.IsNullOrEmpty(three)) 
     { 
      return one + two + three; 
     } 
     return null; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

あなたの答えは正しいですが、なぜこれが修正されたのかについてのより良い説明がこれを答えとして改善するでしょう – MikeT

0

MM8は、正しい答えを提供してきたがしかし、若干の改善が、これは、あなたが複数のコンバー

<local:LogicalAndConverter x:Key="LogicalAndConverter "> 
    <local:LogicalAndConverter.FinalConverter> 
     <BooleanToVisibilityConverter /> 
    </local:LogicalAndConverter.FinalConverter> 
</local:LogicalAndConverter> 
書き換えアウトと異なるコンバータを使用して行うことができます。この

public class LogicalAndConverter : IMultiValueConverter 
{ 
    public IValueConverter FinalConverter{get;set;} 

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool rtn = values.All(v=>(bool)v); 

     if(FinalConverter==null)  
      return rtn; 
     else 
      FinalConverter.Convert(rtn,targetType,parameter,culture); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

だろう

関連する問題