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

时间:2015-11-03 12:25:52

标签: c# xaml windows-phone-8

我正在尝试将背景颜色与包含网格的项目模板绑定。我希望根据状态,列表中每个项目的背景颜色应自动突出显示。所以我这样做,但相应的项目上没有显示颜色。所以如何绑定网格背景,以便改变颜色。 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; }

}

1 个答案:

答案 0 :(得分:0)

您正在公开Brush,但将其绑定为Color。只需更改您的XAML:

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