2017-06-27 2 views
0

Ich habe eine GroupBox deren IsEnabled Eigentum ist über eine Eigenschaft auf dem Ansichtsmodell gesetzt, wie unter: -wpf mehrere Databindings in Groupbox IsEnabled Eigenschaft angeben

<GroupBox> 
    <Canvas IsEnabled="{Binding CurrentRec.Current_Selected_Category.NoBonus,Converter={StaticResource TFC}}"> 
     <Label Content="Amount:" Width="55" Canvas.Left="9" Canvas.Top="-2"/> 
     <TextBox x:Name="txtBonusAmount" Width="76" Canvas.Left="12" Canvas.Top="20" Text="Some text"/> 
     <Label Content="Bonus:" Canvas.Top="38" Width="54" Canvas.Left="10"/> 
     <TextBox x:Name="txtBonus" Width="76" Canvas.Left="13" Canvas.Top="58" Text="Some Text"/> 
    </Canvas> 
<Groupbox> 

Es gibt mehr als eine Eigenschaft in meinem Viewmodel der IsEnabled Eigenschaft zu beeinflussen of Canvas.Wie gebe ich diese zusätzlichen Eigenschaften für die IsEnabled-Eigenschaft von Canvas an?

+0

einen Multibinding mit einem Konverter. – mm8

+0

Verwenden Sie einen Multi-Value-Konverter, wie das Beispiel in [diese Antwort] (https://stackoverflow.com/a/42343062/109702). – slugster

Antwort

2

ein MultiBinding mit einem Konverter verwenden:

<GroupBox> 
    <GroupBox.Resources> 
     <local:MultiConverter x:Key="conv" /> 
    </GroupBox.Resources> 
    <Canvas> 
     <Canvas.IsEnabled> 
      <MultiBinding Converter="{StaticResource conv}"> 
       <Binding Path="CurrentRec.Current_Selected_Category.NoBonus" /> 
       <Binding Path="TheOtherProperty" /> 
      </MultiBinding> 
     </Canvas.IsEnabled> 
     <Label Content="Amount:" Width="55" Canvas.Left="9" Canvas.Top="-2"/> 
     <TextBox x:Name="txtBonusAmount" Width="76" Canvas.Left="12" Canvas.Top="20" Text="Some text"/> 
     <Label Content="Bonus:" Canvas.Top="38" Width="54" Canvas.Left="10"/> 
     <TextBox x:Name="txtBonus" Width="76" Canvas.Left="13" Canvas.Top="58" Text="Some Text"/> 
    </Canvas> 
</GroupBox> 

Die Konverter Klasse sollte implementieren, um die IMultiValueConverter Schnittstelle und Wieder wiederum ein bool vom Convert Methode:

public class MultiConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool noBonus = System.Convert.ToBoolean(values[0]); 
     bool theOtherSourceProperty = System.Convert.ToBoolean(values[1]); 

     //.. 

     return noBonus && theOtherSourceProperty; 
    } 

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

tx mm8.Es hat funktioniert .. :-). –

1
<Window.Resources> 
    <local:OrConverter x:Key="OrConverter" /> 
    <local:AndConverter x:Key="AndConverter" /> 
</Window.Resources> 

...

<Canvas> 
    <Canvas.IsEnabled> 
     <MultiBinding Converter="{StaticResource AndConverter}"> 
      <Binding 
       Path="CurrentRec.Current_Selected_Category.NoBonus" 
       Converter="{StaticResource TFC}" 
       /> 
      <Binding 
       Path="SomeOtherProperty" 
       /> 
     </MultiBinding> 

Wandler:

public class OrConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return values.Select(v => System.Convert.ToBoolean(v)).Any(b => b); 
    } 

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

public class AndConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return values.Select(v => System.Convert.ToBoolean(v)).All(b => b); 
    } 

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