【问题标题】:Binding the background color in a long list selector在长列表选择器中绑定背景颜色
【发布时间】:2015-11-03 20:42: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; }

}

【问题讨论】:

    标签: c# xaml windows-phone-8


    【解决方案1】:

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

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

    【讨论】:

    • 嘿,我也试过了,但还是没有结果
    • @Kam Country 属性的类型是什么?因为HighlightBackgroundColor 应该定义在绑定到 LongListSelector 的同一个对象上
    • country 是我的应用程序的模型,它将国家列表绑定在长列表选择器中。我只希望在列表中选择的国家/地区具有背景颜色变化。
    • 那么HighlightBackgroundColorIsSelected 属性应该在Country 对象上,而不是ListPopup 上。如果您不想将 UI 相关属性添加到模型中,请创建 CountryViewModel 对象并改为绑定它(MVVM 模式)
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2014-02-11
    • 2018-03-04
    • 1970-01-01
    相关资源
    最近更新 更多