Windows 8 绑定到windows 8 gridview中ObservableCollection中的更改对象

Windows 8 绑定到windows 8 gridview中ObservableCollection中的更改对象,windows-8,windows-runtime,winrt-xaml,windows-store-apps,Windows 8,Windows Runtime,Winrt Xaml,Windows Store Apps,我有一个gridview: <GridView xmlns:controls="using:Windows.UI.Xaml.Controls"> <GridView.ItemTemplate> <DataTemplate> <Grid> <Image Source="{Binding Image}"></Im

我有一个gridview:

      <GridView xmlns:controls="using:Windows.UI.Xaml.Controls">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Image Source="{Binding Image}"></Image>
                    <Grid Height="50" Width="50" Background="{Binding Color}"></Grid>
                    <TextBlock FontSize="25" TextWrapping="Wrap" Text="{Binding Name}" Margin="10,10,0,0"/>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
如果在将颜色指定给itemssource之前设置颜色,则效果良好

但是,我还希望能够在指定KeyItem后以编程方式更改其color属性,并让绑定更改颜色。但在这个配置中,这不起作用


让这项工作正常进行的最佳方法是什么?

您的类需要实现。这就是允许绑定知道何时更新的原因。拥有可观察集合只会通知集合的绑定以获得通知。集合中的每个对象也需要实现
INotifyPropertyChanged

注意
NotifyPropertyChanged
中使用的
[CallerMemberName]
。这允许具有默认值的可选参数以调用成员的名称作为其值

public class KeyItem : INotifyPropertyChanged
{
    private string name;
    public string Name 
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            NotifyPropertyChanged();
        }
    }

    private ImageSource image;
    public ImageSource Image 
    {
        get
        {
            return image;
        }
        set
        {
            image = value;
            NotifyPropertyChanged();
        }
    }

    private Brush color;
    public Brush Color
    {
        get
        {
            return color;
        }
        set
        {
            color = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

您的类需要实现。这就是允许绑定知道何时更新的原因。拥有可观察集合只会通知集合的绑定以获得通知。集合中的每个对象也需要实现
INotifyPropertyChanged

注意
NotifyPropertyChanged
中使用的
[CallerMemberName]
。这允许具有默认值的可选参数以调用成员的名称作为其值

public class KeyItem : INotifyPropertyChanged
{
    private string name;
    public string Name 
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            NotifyPropertyChanged();
        }
    }

    private ImageSource image;
    public ImageSource Image 
    {
        get
        {
            return image;
        }
        set
        {
            image = value;
            NotifyPropertyChanged();
        }
    }

    private Brush color;
    public Brush Color
    {
        get
        {
            return color;
        }
        set
        {
            color = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
public class KeyItem : INotifyPropertyChanged
{
    private string name;
    public string Name 
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            NotifyPropertyChanged();
        }
    }

    private ImageSource image;
    public ImageSource Image 
    {
        get
        {
            return image;
        }
        set
        {
            image = value;
            NotifyPropertyChanged();
        }
    }

    private Brush color;
    public Brush Color
    {
        get
        {
            return color;
        }
        set
        {
            color = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}