在长列表选择器中绑定背景颜色

本文关键字:绑定 背景 颜色 选择器 列表 | 更新日期: 2023-09-27 18:20:31

我正在尝试将背景颜色与包含网格的项目模板绑定。我希望根据状态自动突出显示列表中每个项目的背景颜色。所以我正在做这件事,但没有在相应的项目上显示颜色。所以如何绑定网格背景以改变颜色。Xaml代码:

 <phone:LongListSelector x:Name="ResultListBox"  
              Margin="35,10,35,-25"  
             ItemsSource="{Binding Country}" 
              ItemTemplate="{StaticResource CustomList}"
        Height="436" SelectionChanged="ResultListBox_SelectionChanged" Loaded="Listbox_loaded">
 <UserControl.Resources>
   <DataTemplate x:Key="CustomList">
        <Grid Margin="5,0,10,5" Tag="{Binding Name}" x:Name="CountryGrid"  Tap="BorderColor" > 
            <Grid.Background>
                <SolidColorBrush Color="{Binding HighlightBackgroundColor}" />
            </Grid.Background>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="450"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="45"></RowDefinition>
            </Grid.RowDefinitions>

            <Border  VerticalAlignment="Center"  x:Name="HighlightBG" HorizontalAlignment="Left" Grid.Column="0"   Margin="0,5,0,0" Height="70" CornerRadius="0,10,10,0" Grid.Row="0" >
                <StackPanel Orientation="Vertical"   Margin="0,5,0,0" HorizontalAlignment="Center"  >
                    <TextBlock Text="{Binding Name}" x:Name="nametextblock" VerticalAlignment="Top"  TextWrapping="Wrap" Foreground="White" FontSize="30" HorizontalAlignment="Center" ></TextBlock>

                </StackPanel>
            </Border>
        </Grid>
    </DataTemplate>
</UserControl.Resources>


c# code snippet:
 public partial class ListPopup : UserControl,INotifyPropertyChanged
{
    public ListPopup()
    {
        InitializeComponent();
        this.Loaded += ListPopup_Loaded;
        this.IsSelected = false;
        //, INotifyPropertyChanged
        this.NonHighlightColor = new SolidColorBrush(Colors.Transparent);
        this.HighLightColor = new SolidColorBrush(Colors.Red);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    ////////////
    private bool _is_selected;
    public bool IsSelected
    {
        get { return _is_selected; }
        set
        {
            _is_selected = value;
            OnPropertyChanged("HighlightBackgroundColor");
        }
    }
    public SolidColorBrush HighlightBackgroundColor
    {
        get { if (IsSelected) return HighLightColor; else return NonHighlightColor; }
    }
    private SolidColorBrush HighLightColor { get; set; }
    private SolidColorBrush NonHighlightColor { get; set; }
}

在长列表选择器中绑定背景颜色

您公开了一个Brush,但将其绑定为Color。只需更改XAML:

<Grid Margin="5,0,10,5" Background="{Binding HighlightBackgroundColor}" Tag="{Binding Name}" x:Name="CountryGrid"  Tap="BorderColor" >