私はそのようなことについて聞いたことがありませんが、あなたが代わりにコンバータを使用することができます。あなたの最良の選択です。それはきれいで、再利用可能で、容易にプログラム可能です。
ここでは、このようなニーズに対する洗練された解決策があります(もちろん、これは単なる例です)。
ソリューションは、完全に再利用可能にするために構造体を作成します。
public struct NullableBoolColorScheme
{
public Color TrueColor { get; set; }
public Color FalseColor { get; set; }
public Color NullColor { get; set; }
public NullableBoolColorScheme(Color trueColor, Color falseColor, Color nullColor)
{
TrueColor = trueColor;
FalseColor = falseColor;
NullColor = nullColor;
}
public Color GetColor(bool? value)
{
if (!value.HasValue)
return NullColor;
else
return value.Value ? TrueColor : FalseColor;
}
}
は、あなたのコンバータを作成します。
public class NullableBoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool?)) return null;
Color returningColor = Color.Default;
if (parameter != null && parameter is NullableBoolColorScheme)
returningColor = ((NullableBoolColorScheme)parameter).GetColor((value as bool?));
return returningColor;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new Exception("Conversion not allowed");
}
}
はあなたのViewModelでそれをハンドル:
public class MyViewModel // Remember to implement INotifyPropertyChanged and raise property changes
{
public bool? MyNullableBoolProperty
{
get;
set;
}
public NullableBoolColorScheme AppColorScheme { get; }
public MyViewModel()
{
AppColorScheme = new NullableBoolColorScheme(Color.Gray, Color.White, Color.Transparent /* Some possibilities are open here =) */);
}
}
を10
はXAMLでそれを使用してください:
<!-- Declare your namespace -->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converter="clr-namespace:Namespace.Converters"
...
>
<!-- Create your resources dictionary and add a converter instance -->
<ContentPage.Resources>
<ResourceDictionary>
<converter:NullableBoolToColorConverter x:Key="NullableBoolToColorConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<!-- Have fun -->
<Grid Margin="5,0,5,5"
Padding="10"
BackgroundColor="{Binding MyNullableBoolProperty, Mode=OneWay, Converter={StaticResource NullableBoolToColorConverter}, ConverteParameter={Binding AppColorScheme}}">
...
</Grid>
...
</ContentPage>
私はそれはあなたを助ける(とでも英語の間違いを気の毒に)願っています。