Почему не работает привязка?
В DataContext в рантайме передается CameraViewModel, судя по отладчику он действительно там есть, но свойство CurrentFrame не привязывается, причем если пытаться привязаться, например к лейблу и туда текст какой-нибудь писать, то нормально привязывается
<UserControl x:Class="WpfDrawing.View.CameraView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodel="clr-namespace:WpfDrawing.ViewModel"
d:DataContext="{d:DesignInstance Type=viewmodel:CameraViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Image x:Name="cameraFrameElement" Source="{Binding CurrentFrame}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"/>
</Grid>
</UserControl>
VM:
public class CameraViewModel:INotifyPropertyChanged
{
private ImageSource currentFrame;
public ImageSource CurrentFrame
{
get => currentFrame;
set
{
currentFrame = value;
OnPropertyChanged("CurrentFrame");
}
}
public CameraViewModel(CameraManager cameraManager)
{
CameraManager = cameraManager;
CameraManager.PropertyChanged += CameraManager_PropertyChanged;
}
private void CameraManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
CurrentFrame = CameraManager.CurrentFrame;
}
public CameraManager CameraManager { get; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
//if (PropertyChanged != null)
// PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
Ответы (1 шт):
Автор решения: dartvalerius
→ Ссылка
Нужно привязываться к установленным ресурсам
<Image Source="../Resources/ToolBar/document_back.png" />
Или CurrentFrame должен быть типом string в котором прописан путь к файлу изображения