通过数据绑定更改列表框项的背景颜色属性

本文关键字:背景 颜色 属性 列表 数据绑定 | 更新日期: 2023-09-27 18:36:05

我正在尝试制作ListBox,它根据一些变化的数据更新其内容。XAML 如下所示

StackPanel Orientation="Vertical">
<ListBox  x:Name="listWatch"  >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid ShowGridLines="True">
                <Grid Grid.Column="0" Background="{Binding Path=Color">
                    <TextBlock  Text="{ Binding Path=LTP}"  Padding="2 2 2 2"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<Button x:Name="btn"  Click="btn_Click" Content="Button" />

我用于表单数据结构的类如下

 public class WatchRow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    string _color;
    decimal _lTP;
    public WatchRow(decimal LTP,string color)
    {         
        this.LTP = LTP;          
        this.Color = color;
    }
    public string Color 
    {
        get { return _color; }
        set{
            _color = value;
            OnPropertyChanged(_color);
        }
    }       
    public decimal LTP
    {
        get { return _lTP; }
        set
        {
            _lTP = value;
            OnPropertyChanged(_lTP.ToString());
        }
    }  
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
}
 public class Watch:ObservableCollection<WatchRow>
    {
        public Watch():base()
        {
        }       
    }

文件背后的代码就像

    Watch watch = new Watch();
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
    watch.Add(new WatchRow(132, "black"));
    watch.Add(new WatchRow(123, "red"));
    listWatch.ItemsSource = watch;  
watch[0].Color = "green";
}
private void btn_Click(object sender, RoutedEventArgs e)
{
    watch[0].Color = "green";
}

我的问题是我无法通过设置 color 属性(watch[0] 来更改列表框项的颜色。颜色="绿色";)btn_Click,如代码所示。但是相同的代码在PhoneApplicationPage_Loaded_1中有效。我不知道我错了什么。有什么想法吗?

通过数据绑定更改列表框项的背景颜色属性

我相信

问题是您使用PropertyChanged的方式略有变化。调用 OnPropertyChanged 时,请传递要更改的属性的名称,而不是值。例如:

public string Color 
    {
        get { return _color; }
        set{
            _color = value;
            OnPropertyChanged(_color);
        }
    }   

应该是:

public string Color 
    {
        get { return _color; }
        set{
            _color = value;
            OnPropertyChanged("Color");
        }
    }   

另外,我不确定这是否一定是一个问题,但这就是我一直创建 OnPropertyChanged 函数的方式:

而不是:

protected void OnPropertyChanged(string info)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(info));
    }
}

尝试:

private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

此外,正如Magnus Johansson所提到的,定义一个画笔,并绑定颜色,而不是一个字符串。所以 Color 属性将是(有关此的更多详细信息,请参阅他的解释):

private Color _color;
public Color Color 
        {
            get { return _color; }
            set{
                _color = value;
                OnPropertyChanged("Color");
            }
        }   

使用 Mvvm 可以解决您的问题:我已经测试了这段代码,它可以工作。您需要将代码拆分为三个单独的文件,如下所示:

视图模型

public class WatchViewModel
{
    public ObservableCollection<WatchRow> WatchRows { get; set; }
    public void GetWatchRows()
    {
        WatchRows= new ObservableCollection<WatchRow>();
    }
    public void AddWatchRow(int value, string color)
    {
        var item=new WatchRow(value, color);
        WatchRows.Add(item);
    }
}

模型

public class WatchRow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    string _color;
    decimal _lTP;
    public WatchRow(decimal LTP, string color)
    {
        this.LTP = LTP;
        this.Color = color;
    }
    public string Color
    {
        get { return _color; }
        set
        {
            _color = value;
            OnPropertyChanged(_color);
        }
    }
    public decimal LTP
    {
        get { return _lTP; }
        set
        {
            _lTP = value;
            OnPropertyChanged(_lTP.ToString());
        }
    }
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
}

和视图(xaml 页面的代码隐藏)

public partial class MainPage : PhoneApplicationPage
{
    private WatchViewModel watch;
    public MainPage()
    {
        InitializeComponent();
         watch = new WatchViewModel();
        watch.GetWatchRows();

        watch.AddWatchRow(132, "green");
        watch.AddWatchRow(123, "red");
        base.DataContext = watch;
        listWatch.ItemsSource = watch.WatchRows;
    }
    private void btn_Click(object sender, RoutedEventArgs e)
    {
        watch.AddWatchRow(132, "pink");
        watch.AddWatchRow(113, "yellow");
    }
}