如何解决Xamarin表单:如何删除CollectionView的SelectedItem的backgroundcolor xaml 在模型中在ViewModel中
我的CollectionView的ItemSource是一个ToggleButtons列表,每个ToggleButton项都应在第一次单击时进行检查,并设置为第二次单击时未选中。然后问题来了。 问题1:如果单击的项目只是当前选定的项目,则第二次单击将没有任何响应。 对于Q1,我的临时解决方案是这样的,我在DataTemplate的网格上添加了TabGesture,并添加了一些用于处理SelectItem切换的代码。 xaml的部分代码如下,
<CollectionView x:Name="_funBtnsContainer" ItemsSource="{Binding Parasviewmodel.ClickParameters}" ItemsLayout="horizontallist" SelectedItem="{Binding Parasviewmodel.SelectedParamter,Mode=TwoWay}" SelectionMode="Single" >
<CollectionView.Resources>
<Style targettype="Grid">
<Setter Property="visualstatemanager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="normal">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="Black" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="Black" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</CollectionView.Resources>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<customCtrl:ToggleButtonWOGes Margin="0,5,0" WidthRequest="60"
Text="{Binding CaptionExt}" normalStateImage="ic_pan2_common_normal" CheckedImage="ic_pan2_common_selected" disableStateImage="ic_pan2_common_disabled"
IsEnable="{Binding IsEnable}"
Checked="{Binding IsChecked}">
</customCtrl:ToggleButtonWOGes>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
var grid = (Grid)sender;
ObjectParameterValueviewmodel opvv = (ObjectParameterValueviewmodel)grid.BindingContext;
if(opvv == _funBtnsContainer.SelectedItem)
{
_funBtnsContainer.SelectedItem = null;
}else
{
_funBtnsContainer.SelectedItem = opvv;
}
基本上看来可以解决问题。但是发生了另一个问题,就是将SelectedItem设置为null时,总是会有灰色背景闪烁。 我希望删除此灰色背景。如果可以去除灰色背景,我的临时解决方案将是完美的。有谁知道如何去除灰色背景色?
解决方法
由于您已使用MVVM,因此最好处理ViewModel中的逻辑。
在您的情况下,可以通过设置DataTemplate的backgroundColor来实现它。
xaml
将 BackgroundColor 绑定到模型,您无需再设置资源。
<Grid BackgroundColor="{Binding BackgroundColor}">
//...
</Grid>
在模型中
添加权限BackgroundColor
public class MyModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
Color bgColor;
public Color BackgroundColor
{
get
{
return bgColor;
}
set
{
if(bgColor!=value)
{
bgColor = value;
OnPropertyChanged("BackgroundColor");
}
}
}
//... other properties
}
在ViewModel中
MyModel selectedParamter;
public MyModel SelectedParamter
{
get
{
return selectedParamter;
}
set
{
if(selectedParamter != value)
{
selectedParamter = value;
OnPropertyChanged("SelectedParamter");
foreach(MyModel model in ClickParameters)
{
if(model!= SelectedParamter)
{
model.BackgroungColor = Color.Gray; // set background color for other item
}
else
{
model.BackgroungColor = Color.Red; // set background color for selected item
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。