私はPCLを持っており、MVVMパターンを使用しています。 ビヘイビアとトリガを使用して、ビューモデルの関連プロパティがアクティブになったときに切り替えるトグルボタンを作成しています。 これはこれは私が私の行動を消費する方法である私の行動XamarinフォームToggleButton
public class ToggleBehavior : Behavior<View>
{
TapGestureRecognizer tapRecognizer;
public static readonly BindableProperty IsToggledProperty = BindableProperty.Create<ToggleBehavior, bool>(tb => tb.IsToggled, false);
public bool IsToggled
{
set { SetValue(IsToggledProperty, value); }
get { return (bool)GetValue(IsToggledProperty); }
}
}
ある
<Button Text="AUTO" Style="{StaticResource SmallEllipseButton}" BackgroundColor="{StaticResource BackgroundColor}" cm:Message.Attach="Automatic($dataContext)">
<Button.Behaviors>
<local:ToggleBehavior x:Name="autoToggleBehavior" IsToggled="{Binding CurrentMode,Converter={StaticResource modeToBooleanConverter}, ConverterParameter=AUTO}"/>
</Button.Behaviors>
<Button.Triggers>
<DataTrigger TargetType="Button" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="False" >
<Setter Property="BackgroundColor" Value="White"/>
<Setter Property="BorderColor" Value="White"/>
<Setter Property="TextColor" Value="Gray"/>
</DataTrigger>
<DataTrigger TargetType="Button" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="True" >
<Setter Property="BackgroundColor" Value="{StaticResource BackgroundColor}"/>
<Setter Property="TextColor" Value="White"/>
</DataTrigger>
</Button.Triggers>
</Button>
問題は、私がtrueまたはfalseに静的に設定した場合IsToggledこのIsToggled="{Binding CurrentMode,Converter={StaticResource modeToBooleanConverter}, ConverterParameter=AUTO}"/>
に正しくバインドされていないプロパティできます。 私はこのプロパティをdinamicallyバインドすることはできないと思う問題です。 IsVisibleプロパティで同じページ内の同じコンバータで同じバインディングを使用すると、それは機能します。 コンバータにブレークポイントを置くと、アプリケーションは中断されませんが、IsVisibleプロパティはブレークします。
動作しませんバインディング。 true/falseに設定した場合にのみ機能しますが、viewmodelプロパティにバインドできないようです。 – Marco24690
上記のコードは私のバインディングで機能しました。そのトリックは、バインド可能なオブジェクトからOnAttachedTo(BindableObject)のバインディングコンテキストを設定することでした。これは私にとってはハックのようですが、Behaviorがバインド可能でないか、XFコードにバグがあると思われます。 Bugzillaでバグレポートを作成したいかもしれません。 – SKall
OnAttachedToメソッドを追加しませんでした。今それはボタンでも動作します。ありがとう – Marco24690