【发布时间】:2016-01-05 16:45:28
【问题描述】:
也许是愚蠢的问题,但我不知道了......
我有这样的 ViewModel 类:
public class MainWindowsViewModel : INotifyPropertyChanged
{
private ImageSource _img;
public ImageSource StatusImage
{
get { return _img; }
set
{
_img = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName]String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML 中的绑定如下所示:
<Window.DataContext>
<VM:MainWindowsViewModel />
</Window.DataContext>
<Image x:Name="gui_image_status" HorizontalAlignment="Left" Height="26" Margin="144,10,0,0" VerticalAlignment="Top" Width="29" Source="{Binding Path=StatusImage}" />
我这样设置 ImageSource 的内容:
MainWindowsViewModel _view = new MainWindowsViewModel();
var yourImage = new BitmapImage(new Uri(String.Format("Sources/{0}.png", "red"), UriKind.Relative));
_view.StatusImage = yourImage;
但它不起作用。我认为问题在于NotifyPropertyChanged,因为我尝试在set 和get 中放置制动点。 Get 在开始时触发了几次,之后set 也触发了正确的 ImageSource,但之后get 不再触发。就像从未发生过任何设置一样。
真的很简单的绑定,我做过很多次类似的...我不知道为什么这次它不起作用。
【问题讨论】:
标签: c# wpf xaml binding imagesource