0

我正在尝试制作 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 中有效。我不知道我错了什么。有任何想法吗?

4

2 回答 2

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 属性将是(有关此内容的更多详细信息,请参阅他的解释):

private Color _color;
public Color Color 
        {
            get { return _color; }
            set{
                _color = value;
                OnPropertyChanged("Color");
            }
        }   
于 2012-06-01T15:20:59.420 回答
0

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

    }
}
于 2012-06-01T15:44:01.123 回答