XAML Maui绑定背景到集合视图项目错误

mwngjboj  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(132)

我尝试将边框的背景绑定到CollectionView中的LinearGradientBrush,其他项目的背景也会发生变化
公共对象

public partial class CommonObject:ObservableObject
    {
        [ObservableProperty]
        bool showSubmenu = false;

    }

对象

public partial class Product :CommonObject
{

ResourceDictionary rd = App.Current.Resources.MergedDictionaries.First();

 
        [ObservableProperty]
        string id = "";
        [ObservableProperty]
        string name = "";
        [ObservableProperty]
        string description = "";
        [ObservableProperty]
        string categoryId = "";
        [ObservableProperty]
        string barcode = "";
        [ObservableProperty]
        decimal salePriceExclVat = 0.00m;
        [ObservableProperty] 
        decimal salePrice = 0.00m;
        [ObservableProperty]
        string saleTaxRateId = "";
        [ObservableProperty]
        decimal eatOutPriceExclVat = 0.00m;
        [ObservableProperty]
        decimal eatOutPrice = 0.00m;
        [ObservableProperty]
        string eatOutTaxRateId = "";
        [ObservableProperty]
        decimal costPriceExclVat = 0.00m;
        [ObservableProperty]
        decimal costPrice = 0.00m;
        [ObservableProperty]
        string costTaxRateId = "";
        [ObservableProperty]
        string brandId = "";
        [ObservableProperty]
        string supplierId = "";
        [ObservableProperty]
        string popUpInfoId = "";
        [ObservableProperty]
        string mainUnitOfMeasureId = "";
        [ObservableProperty]
        bool trackStock = false;
        [ObservableProperty]
        int posOrder = 0;
        [ObservableProperty]
        bool isSellOnPos = false;
        [ObservableProperty]
        bool isFavorite = false;
        [ObservableProperty]
        bool isSellOnSelfService = false;
        [ObservableProperty]
        bool isSellOnAllLocations = false;
        [ObservableProperty]
        bool isVariablePrice = false;
        [ObservableProperty]
        bool isSoldByWeight = false;
        [ObservableProperty]
        bool isComposite = false;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(IsNotSelected))]
        bool isSelected = false;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(IsSelected))]
        bool isNotSelected = true;
        [ObservableProperty]
        double opacity = 150;
        [ObservableProperty]
        decimal quantity = 0;
        [ObservableProperty]
        Discount discount = null;
        [ObservableProperty]
        bool hasDiscount = false;
        [ObservableProperty]
        bool val = false;
        [ObservableProperty]
        bool proc = false;
        [ObservableProperty]
        LinearGradientBrush bgColor = new LinearGradientBrush();
        [ObservableProperty]
        Color textColor = Color.FromRgb(0, 0, 0);
        [ObservableProperty]
        Color pTextColor = Color.FromRgb(241, 100, 63);
        partial void OnIsSelectedChanged(bool value)
        {
            try
            {
                
                    IsNotSelected = !value;
                    Opacity = value==true ? 0 : 150;
                    if (value)
                    {
                        if (BgColor != (LinearGradientBrush)rd["SelectedItemGradient"])
                            BgColor = rd["SelectedItemGradient"] as LinearGradientBrush;
    
                        if (TextColor != Color.FromRgb(255, 255, 255))
                            TextColor = Color.FromRgb(255, 255, 255);
    
                        if (PTextColor != Color.FromRgb(255, 255, 255))
                            PTextColor = Color.FromRgb(255, 255, 255);
                    }
                    if (!value)
                    {
                        if (BgColor != (LinearGradientBrush)rd["NotSelectedItemGradient"])
                            BgColor = rd["NotSelectedItemGradient"] as LinearGradientBrush;
    
                        if (TextColor != Color.FromRgb(0, 0, 0))
                            TextColor = Color.FromRgb(0, 0, 0);
    
                        if (PTextColor != Color.FromRgb(241, 100, 63))
                            PTextColor = Color.FromRgb(241, 100, 63);
                    }
    
            }
            catch (Exception ex)
            {
    
                ;
            }
    
    
        }
    
    
    }

和产品的CustomView

<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:ObjectModel="clr-namespace:MicroPos.Models.ObjectModels"
             x:DataType="ObjectModel:Product"
              xmlns:ViewModel="clr-namespace:MicroPos.ViewModels"
             xmlns:Converter="clr-namespace:MicroPos.Converters"  
             Margin="0" Padding="0"  BackgroundColor="Transparent"  InputTransparent="False"
             xmlns:xct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        
             x:Class="MicroPos.Views.Custom.ProductView">


    <Grid.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type ViewModel:PosPageViewModel }},Path=AddProductCommand}" CommandParameter="{Binding .}"/>

    </Grid.GestureRecognizers>

    <Grid RowDefinitions="*,30" Margin="0,10,15,0" InputTransparent="True" CascadeInputTransparent="True" IsClippedToBounds="False" HeightRequest="115" WidthRequest="106">


        <Border  Background="{Binding BgColor}" Stroke="Transparent" IsVisible="true" StrokeThickness="0" InputTransparent="True" Grid.Row="0" Grid.RowSpan="2" 
                   
                   Margin="0" Padding="0" HorizontalOptions="Fill" VerticalOptions="Fill">
            <Border.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type ViewModel:PosPageViewModel }},Path=AddProductCommand}" CommandParameter="{Binding .}"/>

            </Border.GestureRecognizers>
            <Border.StrokeShape>
                <RoundRectangle CornerRadius="8,8,8,8" />
            </Border.StrokeShape>
        </Border>

        <Label Text="{Binding Name}" Margin="2,5,2,0" Grid.Row="0" InputTransparent="True"
                   TextColor="{Binding TextColor}"
                   FontSize="15" FontFamily="RobotoMedium" HorizontalOptions="Fill" VerticalOptions="Fill" HorizontalTextAlignment="Center"/>
        <Label Text="{Binding SalePrice}" Grid.Row="1" InputTransparent="True"
                   Margin="0,0,0,5" HorizontalTextAlignment="Center"
                   FontSize="18" FontFamily="RobotoMedium" HorizontalOptions="Center" VerticalOptions="End">
                        <Label.FormattedText>
                            <FormattedString>
                                <Span Text="{Binding SalePrice}"   FontSize="15" FontFamily="RobotoMedium" TextColor="{Binding PTextColor}"/>
                                <Span Text=" RON"   FontSize="13" FontFamily="RobotoMedium" TextColor="{Binding PTextColor}"/>
                            </FormattedString>
                        </Label.FormattedText>
                    </Label>
      
    </Grid>

      <Border BackgroundColor="{StaticResource Main}" InputTransparent="True"
              AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="1,0" 
                   WidthRequest="25" HeightRequest="25" 
                    HorizontalOptions="End" Margin="0,0,2,0" Padding="0" IsVisible="{Binding IsSelected}"
                   VerticalOptions="Start"   Grid.Row="0">
        <Border.StrokeShape>
            <RoundRectangle CornerRadius="25,25,25,25" />
        </Border.StrokeShape>
        <Label Text="{Binding Quantity}" Margin="0" Grid.Row="0" InputTransparent="True"
                   TextColor="White"
                   FontSize="12" FontFamily="RobotoRegular" HorizontalOptions="Fill" VerticalOptions="Fill" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
            </Border>
    
    
    

</Grid>

集合视图

<CollectionView x:Name="collection" ItemsSource="{Binding Products}" ItemTemplate="{Binding ViewType}" HorizontalScrollBarVisibility="Never" 
                        ItemsUpdatingScrollMode="KeepScrollOffset"  Margin="10,0,0,0" ItemSizingStrategy="MeasureFirstItem" 
                         Grid.Row="2" ItemsLayout="{Binding Layout}" HorizontalOptions="FillAndExpand" BackgroundColor="Transparent" />

行为视频
https://youtube.com/shorts/JmHHzm2XoI4
请帮助我,我真的不知道该怎么做,而且collectionview非常落后。

hjzp0vay

hjzp0vay1#

首先,使用setter样式并利用这种视觉状态。

<Style TargetType="Border" x:Key="MyBorderStyle">
    <Setter Property="BackgroundColor" Value="..."/>
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Selected">
                    <VisualState.Setters>                           
                        <Setter Property="BackgroundColor" Value="..."/>                            
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

或者...使用触发器。
第二,我敢打赌你认为CollectionView是滞后的,因为你是在调试。如果你改变到发布,它将加快数量级。总是测试发布性能。你永远不知道有多少跟踪器/记录器在调试中扰乱你的程序。
试试这个,告诉我怎么样。

相关问题