0

以下に示すように、ItemsControl ブロッ​​クに写真のリストがあります。

<ItemsControl Name="icAvatars">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                    <Grid>
                        <TextBlock Visibility="{Binding LdVis}" Text="Loading... "/>
                        <TextBlock Visibility="{Binding ErrVis}" Text="Error while loading the image."/>
                        <Image Source="{Binding ImgSrc}" Visibility="{Binding ImgVis}"/>
                    </Grid>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

新しい画像をリストに追加する必要がある場合はいつでも、オブジェクトが Avatar クラスからインスタンス化され、リストに追加されます。

public class Avatar
{
    public BitmapImage ImgSrc { get; set; }
    public Visibility LdVis { get; set; }
    public Visibility ImgVis { get; set; }
    public Visibility ErrVis { get; set; }
}

Avatar avatar = new Avatar();
var bitmap = new BitmapImage(new Uri(uri));
bitmap.ImageOpened += (s, e) => avatar.ShowImage();
avatar.ImgSrc = bitmap;
icAvatars.Items.Add(avatar);

問題は、画像が読み込まれ、可視性プロパティを (avatar.ImgVis を使用して) 変更しようとすると、アバター オブジェクトの変更が実際の画像に反映されないように見えることです。なぜこれが起こるのですか?

4

2 に答える 2

3

クラスはインターフェイスをAvatar実装する必要があり、プロパティが変更さINotifyPropertyChangedれるたびにイベントが発生する必要があります。実行時に変更できるすべてのデータ バインド プロパティに同じことが適用されます。ImgVisPropertyChanged

public class Avatar : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

    private Visibility _imgVis;

    public Visibility ImgVis 
    { 
       get{ return _imgVis; }
       set
       { 
          if (value == _imgVis) return;
          _imgVis = value;
          NotifyPropertyChanged("ImgVis");
       }
    }
}
于 2013-07-15T13:01:25.320 に答える
1
this is because you have not implement the inotifypropertychange on you avatar class so for doinng that just do like this..
public class Assignment  : INotifyPropertyChanged
{




    private Visibility _ImgVis ;
    public Visibility ImgVis 
    {
        get
        {
            return _ImgVis ;
        }
        set
        {
            _ImgVis  = value;
            FirePropertyChanged("ImgVis ");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void FirePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

アバターのプロパティに加えた変更は常に更新されます..追加および削除時にアバターを変更したい場合は、監視可能なコレクションにします

于 2013-07-15T13:01:55.687 に答える