根据数据绑定值设置背景颜色
Set background color depending on data-bound value
我以前看过一些答案,但没有任何帮助。
我还有一个 class DecideModel
(这将是从数据库中检索的数据集,但为了解决这个问题,我添加了一个 ObservableCollection),其中包含
static DecideModel()
{
All = new ObservableCollection<DecideModel>
{
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 06),
Result = "Maybe"
},
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 05),
Result = "No"
},
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 04),
Result = "Yes"
}
};
}
public DateTime DatePerformed { set; get; }
public string Result { set; get; }
public static IList<DecideModel> All { set; get; }
}
在我的 XAML 代码中
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Maybe">#ffddbc21</Color>
<Color x:Key="Yes">#3CB371</Color>
<Color x:Key="No">#B22222</Color>
<Color x:Key="Depends">#ffd78800</Color>
</ResourceDictionary>
</ContentPage.Resources>
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource {BindingSource Result}}" />
我正在尝试根据从对象获得的结果动态设置标签的背景颜色。
如果您对如何操作有任何想法,请告诉我。我正在寻找任何可用的有用选项。
您可能需要 ValueConverter
。您现在所做的是将背景颜色设置为 'Maybe'、'No' 或 'Yes',这显然不是一种颜色。
您需要做的是将该值转换为颜色。你可以这样做。
创建一个实现 IValueConverter
接口的新 class。它可能看起来像这样:
public class YesNoMaybeToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch(value.ToString().ToLower())
{
case "yes":
return Color.Green;
case "no":
return Color.Red;
case "maybe":
return Color.Orange;
}
return Color.Gray;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// You probably don't need this, this is used to convert the other way around
// so from color to yes no or maybe
throw new NotImplementedException();
}
}
然后将此 class 作为静态资源添加到您的 XAML 页面,如下所示:
<ContentPage.Resources>
<!-- Add this line below -->
<local:YesNoToBooleanConverter x:Key="YesNoMaybeToColorConverter" />
<!-- You can remove the underneath -->
<!--<ResourceDictionary>
<Color x:Key="Maybe">#ffddbc21</Color>
<Color x:Key="Yes">#3CB371</Color>
<Color x:Key="No">#B22222</Color>
<Color x:Key="Depends">#ffd78800</Color>
</ResourceDictionary>-->
</ContentPage.Resources>
现在在你的绑定中你必须告诉他使用什么转换器。这样做:
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{Binding Result, Converter={StaticResource YesNoMaybeToColorConverter}}" />
它现在应该在 Result
字段中看到值,将其通过您定义的转换器和 return 您与该值对应的颜色。
我找到了 2 个其他选项来管理它,因为有时我们不仅需要更改颜色,还需要更改字体和其他值。
首先,您需要像这样向控件添加名称:
<Label x:Name="MyLabel" Text="{Binding Result}" HorizontalOptions="FillAndExpand"/>
1. 你可以像这样在代码后面设置颜色和其他属性,这种方式需要一些时间更新,所以不是最好的选择,当 Text 属性 刚刚更新。
protected override void OnAppearing()
{
if (this.MyLabel.Text.Contains("yes")
{
this.MyLabel.TextColor = Color.FromHex("#98ee99");
}
else
{
this.MyLabel.TextColor = Color.FromHex("#ff867c");
}
}
2. 另一种方法是使用 Prism EventAggregator,我猜在 Xamarin.Forms 中它是 Messaging Center。这是一个 good example with Prism.
在这种情况下,您应该从项目中的任何位置发送带有您的值的事件,例如当它应该已经更新时。
_evenAggregator.GetEvent<MyEvent>().Publish(Result);
那么您应该在需要接收新值的地方订阅事件。在这种情况下,它应该是 class.
后面的代码中的 OnAppearing 方法
void UpdateColor(string result)
{
if(result.Contains("yes")
{
this.MyLabel.TextColor = Color.FromHex("#98ee99");
}
else
{
this.MyLabel.TextColor = Color.FromHex("#ff867c");
}
}
protected override void OnAppearing()
{
base.OnAppearing();
_eventAggregator.GetEvent<MyEvent>().Subscribe(UpdateColor);
}
要以一种纯粹的 XAML 方式实现此目的而无需太多开销,您可以使用 DataTrigger
。请注意,您可以根据需要为每个触发器添加尽可能多的设置器,这比之前建议的解决方案稍微灵活一些,它还将视图逻辑保留在视图中它应该在的位置。
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Yes">
<Setter Property="BackgroundColor" Value="#3CB371" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="No">
<Setter Property="BackgroundColor" Value="#B22222" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Maybe">
<Setter Property="BackgroundColor" Value="#ddbc21" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Depends">
<Setter Property="BackgroundColor" Value="#d78800" />
</DataTrigger>
</Label.Triggers>
</Label>
请注意,您可以通过将 BackgroundColor
属性 设置为合理的默认值(在本例中可能是 "Depends")来切断其中一个触发器。
我以前看过一些答案,但没有任何帮助。
我还有一个 class DecideModel
(这将是从数据库中检索的数据集,但为了解决这个问题,我添加了一个 ObservableCollection),其中包含
static DecideModel()
{
All = new ObservableCollection<DecideModel>
{
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 06),
Result = "Maybe"
},
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 05),
Result = "No"
},
new DecideModel
{
DatePerformed = new DateTime(2015, 4, 04),
Result = "Yes"
}
};
}
public DateTime DatePerformed { set; get; }
public string Result { set; get; }
public static IList<DecideModel> All { set; get; }
}
在我的 XAML 代码中
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Maybe">#ffddbc21</Color>
<Color x:Key="Yes">#3CB371</Color>
<Color x:Key="No">#B22222</Color>
<Color x:Key="Depends">#ffd78800</Color>
</ResourceDictionary>
</ContentPage.Resources>
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource {BindingSource Result}}" />
我正在尝试根据从对象获得的结果动态设置标签的背景颜色。
如果您对如何操作有任何想法,请告诉我。我正在寻找任何可用的有用选项。
您可能需要 ValueConverter
。您现在所做的是将背景颜色设置为 'Maybe'、'No' 或 'Yes',这显然不是一种颜色。
您需要做的是将该值转换为颜色。你可以这样做。
创建一个实现 IValueConverter
接口的新 class。它可能看起来像这样:
public class YesNoMaybeToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch(value.ToString().ToLower())
{
case "yes":
return Color.Green;
case "no":
return Color.Red;
case "maybe":
return Color.Orange;
}
return Color.Gray;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// You probably don't need this, this is used to convert the other way around
// so from color to yes no or maybe
throw new NotImplementedException();
}
}
然后将此 class 作为静态资源添加到您的 XAML 页面,如下所示:
<ContentPage.Resources>
<!-- Add this line below -->
<local:YesNoToBooleanConverter x:Key="YesNoMaybeToColorConverter" />
<!-- You can remove the underneath -->
<!--<ResourceDictionary>
<Color x:Key="Maybe">#ffddbc21</Color>
<Color x:Key="Yes">#3CB371</Color>
<Color x:Key="No">#B22222</Color>
<Color x:Key="Depends">#ffd78800</Color>
</ResourceDictionary>-->
</ContentPage.Resources>
现在在你的绑定中你必须告诉他使用什么转换器。这样做:
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{Binding Result, Converter={StaticResource YesNoMaybeToColorConverter}}" />
它现在应该在 Result
字段中看到值,将其通过您定义的转换器和 return 您与该值对应的颜色。
我找到了 2 个其他选项来管理它,因为有时我们不仅需要更改颜色,还需要更改字体和其他值。 首先,您需要像这样向控件添加名称:
<Label x:Name="MyLabel" Text="{Binding Result}" HorizontalOptions="FillAndExpand"/>
1. 你可以像这样在代码后面设置颜色和其他属性,这种方式需要一些时间更新,所以不是最好的选择,当 Text 属性 刚刚更新。
protected override void OnAppearing()
{
if (this.MyLabel.Text.Contains("yes")
{
this.MyLabel.TextColor = Color.FromHex("#98ee99");
}
else
{
this.MyLabel.TextColor = Color.FromHex("#ff867c");
}
}
2. 另一种方法是使用 Prism EventAggregator,我猜在 Xamarin.Forms 中它是 Messaging Center。这是一个 good example with Prism.
在这种情况下,您应该从项目中的任何位置发送带有您的值的事件,例如当它应该已经更新时。
_evenAggregator.GetEvent<MyEvent>().Publish(Result);
那么您应该在需要接收新值的地方订阅事件。在这种情况下,它应该是 class.
后面的代码中的 OnAppearing 方法void UpdateColor(string result)
{
if(result.Contains("yes")
{
this.MyLabel.TextColor = Color.FromHex("#98ee99");
}
else
{
this.MyLabel.TextColor = Color.FromHex("#ff867c");
}
}
protected override void OnAppearing()
{
base.OnAppearing();
_eventAggregator.GetEvent<MyEvent>().Subscribe(UpdateColor);
}
要以一种纯粹的 XAML 方式实现此目的而无需太多开销,您可以使用 DataTrigger
。请注意,您可以根据需要为每个触发器添加尽可能多的设置器,这比之前建议的解决方案稍微灵活一些,它还将视图逻辑保留在视图中它应该在的位置。
<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Yes">
<Setter Property="BackgroundColor" Value="#3CB371" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="No">
<Setter Property="BackgroundColor" Value="#B22222" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Maybe">
<Setter Property="BackgroundColor" Value="#ddbc21" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Depends">
<Setter Property="BackgroundColor" Value="#d78800" />
</DataTrigger>
</Label.Triggers>
</Label>
请注意,您可以通过将 BackgroundColor
属性 设置为合理的默认值(在本例中可能是 "Depends")来切断其中一个触发器。