2015-11-03 22 views
0

我想綁定背景顏色與包含網格的項目模板。我希望根據狀態自動突出顯示列表中每個項目的背景顏色。所以我這樣做,但沒有顏色顯示在各自的item.so如何綁定網格背景,使顏色改變。 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; } 

} 

回答

0

你露出Brush但結合它作爲一個Color。只要改變你的XAML:

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

嘿,我已經嘗試過這種方式太多,但還是沒有結果 – Kam

+0

@Kam什麼是'Country'屬性的類型?因爲'HighlightBackgroundColor'應該在與LongListSelector綁定的同一對象上定義 –

+0

country是我的應用程序的模型,它綁定了長列表選擇器中的國家/地區列表。我只想在列表中選擇的國家有背景顏色變化。 – Kam

相關問題