选择其他按钮时,ListView 突出显示所选项目颜色不会停留
ListView Highlight Selected Item Color is not staying on when selected other buttons
我有这个带有 2 个按钮的 ListView,每个 List/Item(加号和减号按钮)仅出现在 selected 项目上。
除了这些按钮之外,selected 项目还将颜色更改为浅灰色,预览项目将变为透明(效果很好)。
但我遇到的问题是,当我 select 一个项目然后单击加号/减号按钮时,它会清除 selected 项目颜色。
所以只有按钮会保留,但 BackgroundColor 消失了。
我试图一直保持 LightGray 颜色,直到我 select 一个不同的项目。
因为我是新手如果你能帮我举个例子就太好了(如果可能的话)。
如果需要更多信息,请告诉我。
非常感谢。
ItemsPage.xaml
<ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="0,0,8,0" Margin="4,0,4,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Edit1" CommandParameter="{Binding ItemId}"/>
</Grid.GestureRecognizers>
<Label Text="{Binding ItemName}" Grid.Column="1" FontSize="Medium"></Label>
<Image Source="DecreaseIcon1.png" Grid.Column="2" Margin="4" IsVisible="{Binding IsVisible}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="DecrementQuantity" CommandParameter="{Binding ItemId}"/>
</Image.GestureRecognizers>
</Image>
<Entry Text="{Binding ItemQuantity}" Grid.Column="3" VerticalTextAlignment="Center"/>
<Image Source="IncreaseIcon1.png" Grid.Column="4" Margin="4" IsVisible="{Binding IsVisible}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="IncrementQuantity" CommandParameter="{Binding ItemId}"/>
</Image.GestureRecognizers>
</Image>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ItemsPage.cs
private void IncrementQuantity(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
var oldQuantity = item.ItemQuantity++;
}
private void DecrementQuantity(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
var oldQuantity = item.ItemQuantity--;
}
Grid lastGrid;
private void TapGestureRecognizer_Edit1(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
_item = item;
var grid = sender as Grid;
lstView.SelectedItem = grid.BindingContext;
if (lastGrid != null)
{
lastGrid.BackgroundColor = Color.Transparent;
}
var viewCell = (Grid)sender;
if (viewCell.BackgroundColor != null)
{
viewCell.BackgroundColor = Color.LightGray;
lastGrid = viewCell;
}
}
好吧,当您点击任何其他视图(如按钮或其他内容)时,列表视图会失去焦点。当列表视图未聚焦时,其背景颜色将丢失。
但是,尝试直接设置 listView 的背景颜色可能会满足您的要求。 注意:我还没有在我的电脑上试过这段代码,看它是否能解决你的问题,但试一试,让我知道你的结果。
if (viewCell.BackgroundColor != null)
{
lstView.BackgroundColor = Color.LightGray;
// Other lines of code
}
根据你的描述,我建议你可以使用CollectionView to replace ListView. Then using Xamarin.Forms Visual State Manager高亮选中项的背景色。
这是 ContentPage.Resource 中的 VisualStateManager。
<ContentPage.Resources>
<Style x:Key="stacklayoutStyle" TargetType="StackLayout">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
在 CollectionView.The 选中状态下使用此样式将在您单击按钮时保持打开状态。
<CollectionView ItemsSource="{Binding items}" SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Style="{StaticResource stacklayoutStyle}">
<Grid Margin="4,0,4,0" Padding="0,0,8,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="1"
FontSize="Medium"
Text="{Binding ItemName}" />
<Button Grid.Column="2" Text="decrement" />
<Entry
Grid.Column="3"
Text="{Binding ItemQuantity}"
VerticalTextAlignment="Center" />
<Button Grid.Column="4" Text="Increment" />
</Grid>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
我有这个带有 2 个按钮的 ListView,每个 List/Item(加号和减号按钮)仅出现在 selected 项目上。 除了这些按钮之外,selected 项目还将颜色更改为浅灰色,预览项目将变为透明(效果很好)。
但我遇到的问题是,当我 select 一个项目然后单击加号/减号按钮时,它会清除 selected 项目颜色。 所以只有按钮会保留,但 BackgroundColor 消失了。
我试图一直保持 LightGray 颜色,直到我 select 一个不同的项目。
因为我是新手如果你能帮我举个例子就太好了(如果可能的话)。
如果需要更多信息,请告诉我。
非常感谢。
ItemsPage.xaml
<ListView ItemsSource="{Binding Items, Mode=TwoWay}" x:Name="lstView" SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="0,0,8,0" Margin="4,0,4,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Edit1" CommandParameter="{Binding ItemId}"/>
</Grid.GestureRecognizers>
<Label Text="{Binding ItemName}" Grid.Column="1" FontSize="Medium"></Label>
<Image Source="DecreaseIcon1.png" Grid.Column="2" Margin="4" IsVisible="{Binding IsVisible}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="DecrementQuantity" CommandParameter="{Binding ItemId}"/>
</Image.GestureRecognizers>
</Image>
<Entry Text="{Binding ItemQuantity}" Grid.Column="3" VerticalTextAlignment="Center"/>
<Image Source="IncreaseIcon1.png" Grid.Column="4" Margin="4" IsVisible="{Binding IsVisible}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="IncrementQuantity" CommandParameter="{Binding ItemId}"/>
</Image.GestureRecognizers>
</Image>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ItemsPage.cs
private void IncrementQuantity(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
var oldQuantity = item.ItemQuantity++;
}
private void DecrementQuantity(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
var oldQuantity = item.ItemQuantity--;
}
Grid lastGrid;
private void TapGestureRecognizer_Edit1(object sender, EventArgs e)
{
TappedEventArgs tappedEventArgs = (TappedEventArgs)e;
Item item = ((ItemViewModel)BindingContext).Items.Where(x => x.ItemId == (Guid)tappedEventArgs.Parameter).FirstOrDefault();
_item = item;
var grid = sender as Grid;
lstView.SelectedItem = grid.BindingContext;
if (lastGrid != null)
{
lastGrid.BackgroundColor = Color.Transparent;
}
var viewCell = (Grid)sender;
if (viewCell.BackgroundColor != null)
{
viewCell.BackgroundColor = Color.LightGray;
lastGrid = viewCell;
}
}
好吧,当您点击任何其他视图(如按钮或其他内容)时,列表视图会失去焦点。当列表视图未聚焦时,其背景颜色将丢失。
但是,尝试直接设置 listView 的背景颜色可能会满足您的要求。 注意:我还没有在我的电脑上试过这段代码,看它是否能解决你的问题,但试一试,让我知道你的结果。
if (viewCell.BackgroundColor != null)
{
lstView.BackgroundColor = Color.LightGray;
// Other lines of code
}
根据你的描述,我建议你可以使用CollectionView to replace ListView. Then using Xamarin.Forms Visual State Manager高亮选中项的背景色。
这是 ContentPage.Resource 中的 VisualStateManager。
<ContentPage.Resources>
<Style x:Key="stacklayoutStyle" TargetType="StackLayout">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="LightGray" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
在 CollectionView.The 选中状态下使用此样式将在您单击按钮时保持打开状态。
<CollectionView ItemsSource="{Binding items}" SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Style="{StaticResource stacklayoutStyle}">
<Grid Margin="4,0,4,0" Padding="0,0,8,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="1"
FontSize="Medium"
Text="{Binding ItemName}" />
<Button Grid.Column="2" Text="decrement" />
<Entry
Grid.Column="3"
Text="{Binding ItemQuantity}"
VerticalTextAlignment="Center" />
<Button Grid.Column="4" Text="Increment" />
</Grid>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>