C# ItemsControl中的项未更新
我有一个ItemsControl块中的图片列表,如下所示C# ItemsControl中的项未更新,c#,wpf,windows-phone-8,C#,Wpf,Windows Phone 8,我有一个ItemsControl块中的图片列表,如下所示 <ItemsControl Name="icAvatars"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Grid> <
<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.ImgVis)时,对avatar对象的更改似乎没有传播到实际图像。为什么会发生这种情况?您的
Avatar
类应该实现接口,并且每次ImgVis
属性更改时都应该引发PropertyChanged
事件。同样的事情也应该适用于所有可以在运行时更改的数据绑定属性
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");
}
}
}
您的
Avatar
类应该实现接口,并且每次ImgVis
属性更改时都应该引发PropertyChanged
事件。同样的事情也应该适用于所有可以在运行时更改的数据绑定属性
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");
}
}
}
它将始终更新您对头像属性所做的任何更改。如果您想添加和删除头像上的更改…请将其设置为可观察的集合
它将始终更新您对头像属性所做的任何更改。如果您想添加和删除头像上的更改…请将其设置为可观察的集合尝试在元素中的xaml代码中使用
Mode=TwoWay
。我尝试过了。未更改。您在哪里为可见性属性指定值。。??Ldvis和errvis尝试调用RaisePropertyChanged事件…我在ImageOpened的事件处理程序中执行此操作。您可以看到已编辑的代码。请尝试在元素中的xaml代码中使用Mode=TwoWay
。我已经尝试过了。未更改。您在哪里为可见性属性指定值。。??Ldvis和errvis尝试调用RaisePropertyChanged事件…我在ImageOpened的事件处理程序中执行此操作。您可以看到已编辑的代码。did在同一时间:)did在同一时间:)很抱歉,我只能选择一个正确答案。无论如何,非常感谢:)对不起,我只能选择一个正确的答案。无论如何,非常感谢:)
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));
}
}
}