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

时间:2012-06-01 14:57:19

标签: c# silverlight windows-phone-7 xaml

我正在尝试使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";
}

我的问题是我无法通过在btn_Click中设置颜色属性( watch [0] .Color =“green”; )来更改列表框项的颜色,如下所示代码。但相同的代码适用于PhoneApplicationPage_Loaded_1。我不知道我错了什么。任何想法?

2 个答案:

答案 0 :(得分:0)

我认为问题是你如何使用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,而不是字符串。因此Color属性将是(有关此内容的详细信息,请参阅他的解释):

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

答案 1 :(得分:0)

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

viewModel

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");

    }
}