【发布时间】:2021-09-29 14:38:03
【问题描述】:
我想更改框架的背景颜色。我尝试了很多方法,例如 设置 Focused 事件、触发器和背景颜色属性绑定 对我没有任何作用。这是我的 xaml 代码。
<StackLayout Padding="10">
<CollectionView x:Name="list" ItemsSource="{Binding samplelist}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="2" HorizontalItemSpacing="10" VerticalItemSpacing="10" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Green" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Frame CornerRadius="10" HasShadow="False" BackgroundColor="{Binding BackgroundTest,Mode=TwoWay}" HeightRequest="75" Margin="5,0,0,0" >
<StackLayout Orientation="Vertical">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference test}, Path=BindingContext.textselected}"
CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
这是我在视图模型中的代码
private string _backgroundTest;
public string BackgroundTest
{
get { return _backgroundTest; }
// set => SetProperty(ref _backgroundTest, value);
set
{
if (value == _backgroundTest)
return;
_backgroundTest = value;
OnPropertyChanged(nameof(BackgroundTest));
}
}
private async void clicked(Test test)
{
BackgroundTest = "#F95F62";
}
我参考了以下链接 How to change color of Frame control when clicked in Xamarin.Forms with MVVM
Xamarin how to change background color on button click MVVM
但没有任何效果。我不知道如何解决这个问题。有什么建议吗?
【问题讨论】:
-
BackgroundColor 需要一个颜色,但您返回的是一个字符串。尝试使用转换器,或者只使用颜色属性而不是字符串
标签: c# android xamarin xamarin.forms frame