Caliburn Micro - 更改BindableCollection中的单个项目

时间:2012-05-25 16:58:02

标签: wpf caliburn.micro

我在列表视图中显示一个集合,每个listview项目都有自己的按钮,用于下载相关的pdf。

我想点击按钮并在其中显示一个不确定的进度条。

在我的示例项目中,我只是在单击其按钮时尝试反转项目的下载状态。

我的viewmodel代码:

private BindableCollection<IDownloadable> _downloadables;
public BindableCollection<IDownloadable> Downloadables
{
    get { return _downloadables; }
    set
    {
        _downloadables = value;
        NotifyOfPropertyChange(() => Downloadables);
    }
}

public void Download(IDownloadable item)
{
    item.Downloading = !item.Downloading;
    NotifyOfPropertyChange(() => Downloadables);
}

public ShellViewModel()
{
    Downloadables = new BindableCollection<IDownloadable>();

    Downloadables.Add(new DownloadString("String"));
    Downloadables.Add(new DownloadString("String"));
    Downloadables.Add(new DownloadString("String"));
}

我的观点:

<ListView Name="Downloadables"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Background="White">
                            <ToggleButton Content="{Binding MyString}"
                                          Width="50"
                                          Height="50"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center" 
                                          cal:Message.Attach="Download($dataContext)"/>
                            <spark:SprocketControl Width="30"
                                                   Height="30"
                                                   HorizontalAlignment="Center"
                                                   VerticalAlignment="Center"
                                                   AlphaTicksPercentage="50"
                                                   Interval="60"
                                                   IsIndeterminate="True"
                                                   LowestAlpha="50"
                                                   StartAngle="-90"
                                                   TickColor="LawnGreen"
                                                   TickCount="12"
                                                   TickWidth="3" 
                                                   Visibility="{Binding Downloading, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                        </Grid>

                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

我曾想过NotifyOfPropertyChanged会告诉收集项目更新,但情况似乎并非如此。

1 个答案:

答案 0 :(得分:3)

这与收藏无关。模板绑定到项目本身。它不知道该集合。这意味着,您需要在项目本身中实现通知(INotifyPropertyChanged)。