Image source not updating dynamically - silverlight

I have an Image that I have bound the 'Source' property of to the page's corresponding 'ViewModel' property named 'Capture' as follows:
View Code:
<Image Height="150" HorizontalAlignment="Left" Margin="50,583,0,0" x:Name="img_FlickrPic" Stretch="Fill" VerticalAlignment="Top" Width="200" Grid.Row="1" Source="{Binding Capture}"/>
Corresponding ViewModel code
public class SubmitViewModel : ViewModelBase
{
private CameraCaptureTask cameraCapture;
public ImageSource Capture { get; set; }
public RelayCommand CaptureCommand { get; set; }
/// <summary>
/// Initializes a new instance of the SubmitViewModel class.
/// </summary>
public SubmitViewModel()
{
Capture = new BitmapImage();
CaptureCommand = new RelayCommand(() => CapturePhoto());
}
private object CapturePhoto()
{
cameraCapture = new CameraCaptureTask();
cameraCapture.Completed += cameraCapture_Completed;
cameraCapture.Show();
return null;
}
void cameraCapture_Completed(object sender, PhotoResult e)
{
if (e == null || e.TaskResult != TaskResult.OK)
{
return;
}
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
Capture = bitmap;
}
}
As you notice, I have bound a capture button to the view model as well using 'behavior's instead of a click event in order to keep the code behind clean. When I hit the button, the camera gets invoked and once I hit capture and then press the 'Accept' button the 'cameraCapture_Completed' event fires as expected and the code in there executes. However the last step where the 'Capture' property (which my Image's Source property on the view is bound to) is set, I expect the Image to dynamically update with the captured photo. This does not happen. The viewmodel inherits from 'ViewModelBase' which in turn implements INotifyPropertyChanged, so that shouldn't be a problem. Why aren't any modifications to the 'Capture' property being reflected by the Image in the UI? Am I messing up somewhere here?
Thanks!

You have to manually raise the ChangedPropertyEvent with property name "Capture". Something like this.
ImageSource mCapture;
public ImageSource Capture {
get {return mCapture;}
set { mCapture = value;
RaiseChangedProperty("Capture");
}
}

Related

Event handler not firing on property changed

I have a WPF application and I want the Start button control only enabled if they have to have specified a value in the text box for 'Download Path'.
My ViewModel contains a property for my model "ConfigurationSettings" and an ICommand implementation (CommandImp) for the button:
public class MainWindowViewModel : BaseNotifyPropertyChanged
{
private ConfigurationSettings _configurationSettings { get; set; }
public ConfigurationSettings ConfigurationSettings
{
get
{
return _configurationSettings;
}
set
{
if (_configurationSettings != value)
{
_configurationSettings = value;
RaisePropertyChanged("ConfigurationSettings");
}
}
}
public CommandImp StartCommand { get; set; } // this is an implementation of ICommand
public MainWindowViewModel()
{
StartCommand = new CommandImp(OnStart, CanStart);
_configurationSettings = new ConfigurationSettings();
_configurationSettings.PropertyChanged += delegate (object o,
PropertyChangedEventArgs args)
{
StartCommand.RaiseCanExecuteChanged(); // break point here is never reached
};
}
private bool CanStart()
{
if (!String.IsNullOrEmpty(ConfigurationSettings.DownloadPath))
{
return true;
}
return false;
}
}
In my XAML I have a Start button and the with Command = "{Binding StartCommand}".
My ConfigurationSettings class just has a string for the DownloadPath which is bound to a textbox in the XAML:
public class ConfigurationSettings : BaseNotifyPropertyChanged
{
private string _downloadPath { get; set; }
public string DownloadPath
{
get { return _downloadPath; }
set
{
if (_downloadPath != value)
{
_downloadPath = value;
RaisePropertyChanged("DownloadPath"); // break point here IS reached
}
}
}
}
When the user enters a DownloadPath, I expect it to be triggering the PropertyChanged Event, and running my delegate method defined in the ViewModel constructor.
If I move the Command Button inside the ConfigurationSettings class I can do away with event subscription and just use StartCommand.RaiseCanExecuteChanged() right beneath RaisePropertyChanged("DownloadPath");. But I don't want the ICommand as part of my Model.
How can I trigger CanStart() when one of the properties of ConfigurationSettings changes?
UPDATE:
Here is the XAML for the text box binding:
<TextBlock Text="{Binding ConfigurationSettings.DownloadPath, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" TextWrapping="WrapWithOverflow" />
And the button:
<Button Content="Start" Command="{Binding StartCommand}"></Button>
I should note that the bindings are working correctly. When I update the textblock, I can see in the ViewModel that ConfigurationSettings.DownloadPath is correctly being updated.
BaseNotifyPropertyChanged is an implementation of INotifyPropertyChanged like so:
public class BaseNotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
I don't seem to be having any issues with the property changed event. I can put a break point in here and it is hit when I update the DownloadPath text box. It's when I subscribe to this PropertyChanged event in my ViewModel constructor, my delegate method isn't firing.
Hate to answer my own question but the people commenting made me think about restructuring my question - which led me to the answer before needing to make another update.
The solution was to move my event subscription inside the 'set' function for ConfigurationSettings:
private ConfigurationSettings _configurationSettings { get; set; }
public ConfigurationSettings ConfigurationSettings
{
get
{
return _configurationSettings;
}
set
{
if (_configurationSettings != value)
{
_configurationSettings = value;
_configurationSettings = new Model.ConfigurationSettings();
_configurationSettings.PropertyChanged += (o, args) =>
{
StartCommand.RaiseCanExecuteChanged();
};
RaisePropertyChanged("ConfigurationSettings");
}
}
}
The problem was where I was setting my Data Context which I did not originally suspect was at all the problem. I load the view model from an XML file on disk. And when the application is closed, I overwrite that file with the latest ViewModel.
In the constructor I was reading and setting the DataContext:
public MainWindowView()
{
InitializeComponent();
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
DataSourcePath = new Uri(System.IO.Path.Combine(appPath, DataFileName));
if (File.Exists(DataSourcePath.LocalPath))
{
XmlReader reader = XmlReader.Create(DataSourcePath.LocalPath);
DataContext = (MainWindowViewModel)serialize.Deserialize(reader);
reader.Close();
}
else
{
WriteDataViewModelToDisk(); // new empty view model written to disk
}
}
If this was the first time I ran the code, with no pre-existing file, my delegate event handler actually worked. The issue was when this code loaded a pre-existing XML file, it overwrote the ConfigurationSettings property in my view model - thus destroying the event subscription.

Passing complex object to a Child Window

What's the right approach to open a child window (for example, to modify a selected item on the main window) keeping MVVM in mind?
Here's what I have: MainWindow.xaml (and in MainWindow.xaml.cs it assigns MainVM as its own DataContext)
I would also like to have: ChildWindow.xaml and barebones ChildWindow.xaml.cs with ChildVM behind controls.
So, now:
How can I popup ChildWindow and pass some object Data to its
ChildVM?
Get the result (true/false) and result data (some complex
object) back to MainVM?
As a bonus, can changes in Data be observed
by MainVM while they are being worked on by ChildVM?
Here's what I tried - it doesn't solve everything, but is this even the right direction?
For (2), I created a subclass of Window, called DialogWindow, which has 3 DependencyProperties: Data (for input data), ResultData (for output data) and ResultValue (for a bool result).
ResultData and ResultValue are both set by the ChildVM of DialogWindow using Binding, and when ResultValue is set, the DialogWindow closes.
At the moment, the ChildWindow is launched (for all intents and purposes) from MainWindow.xaml.cs - kinda bad. I can then pass some input data, like so:
ChildDialogWindow w = new ChildDialogWindow();
w.Data = myDataObj;
So, now I need to have a property Data on ChildVM, and set in ChildDialogWindow.xaml.cs. Again, making .xaml.cs thicker.
I thought that maybe a better approach that avoids MainWindow.xaml.cs would be some kind of DialogService which is passed to MainVM as a dependency. But then, how can I pass values to the ChildVM?
Try this.
Make a DialogService.cs
public class DialogService
{
public void Show(FrameworkElement view, ChildViewModel ChildVM)
{
Window window = new Window();
window.Content = view;
window.DataContext = ChildVM;
// For closing this dialog using MVVM
ChildVM.RequestClose += delegate
{
window.Close();
};
window.Show();
}
}
Now in ChildVm class, add this
public ICommand CloseCommand
{
get
{
if (_closeCommand == null)
_closeCommand = new RelayCommand(param => this.OnRequestClose());
return _closeCommand;
}
}
public event EventHandler RequestClose;
void OnRequestClose()
{
EventHandler handler = this.RequestClose;
if (handler != null)
handler(this, EventArgs.Empty);
}
Now, this the way to launch this
public void OpenChildDailog()
{
DialogService service = new DialogService();
ChildViewModel childVM = new ChildViewModel();
childVM.Data = ; // Assign whatever you want
childVM.ResultData = ;
service.Show(new ChildView(), childVM);
// Now get the values when the child dailog get closed
var retVal = childVM.ResultValue;
}
I'm using the ICommand helper "RelayCommand," and pushing an IntPtr datatype to the new ViewModel (or use any other object.) Lots of cookie cutter stuff.
Main View:
<Button Command="{Binding DataContext.ShowObjectInfoCommand}" CommandParameter="{Binding ObjectOffset}" Content="{Binding Name}"/>
MainViewModel:
private RelayCommand _showObjectInfoCommand;
public RelayCommand ShowObjectInfoCommand { get { return _showObjectInfoCommand ?? (_showObjectInfoCommand = new RelayCommand(ExeShowObjectInfoCommand)); } set { } } //Draw Specific Item Table
void ExeShowObjectInfoCommand(object parameter)
{
ViewObjectInfo objInfo = new ViewObjectInfo();
IObjectOffsetParameter viewModel = objInfo.DataContext as IObjectOffsetParameter;
viewModel.ObjectOffset = (IntPtr)parameter;
objInfo.Show();
}
New ViewModel + interface:
interface IObjectOffsetParameter
{
IntPtr ObjectOffset { get; set; }
}
class ViewModelObjectInfo : ViewModelBase, IObjectOffsetParameter
{
public ViewModelObjectInfo()
{
}
private IntPtr _objectOffset; //Entity Offset
public IntPtr ObjectOffset
{
get { return _objectOffset; }
set { if (_objectOffset != value) { _objectOffset = value; RaisePropertyChanged("Offset"); } }
}
}
New View code-behind:
InitializeComponent();
ViewModelObjectInfo viewModel = new ViewModelObjectInfo();
this.DataContext = viewModel;
New View xaml:
<TextBlock Text="{Binding ObjectOffset}"/>

How do I bind DateTime as DepedencyProperty of a DependencyObject so that it will show up in a textbox?

I want to learn how to use Dependency Objects and Properties. I have created this class,
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty =
DependencyProperty.Register("TestDateTime",
typeof(DateTime),
typeof(TestDependency),
new PropertyMetadata(DateTime.Now));
public DateTime TestDateTime
{
get { return (DateTime) GetValue(TestDateTimeProperty); }
set { SetValue(TestDateTimeProperty, value); }
}
}
The window class is like this
public partial class MainWindow : Window
{
private TestDependency td;
public MainWindow()
{
InitializeComponent();
td = new TestDependency();
td.TestDateTime = DateTime.Now;
}
}
Now I want to use it to show a the current DateTime in the TextBlock which updates itself every second, by adding this to a grid
<Grid>
<TextBlock Text="{Binding TestDateTime,ElementName=td}" Width="200" Height="200"/>
</Grid>
I can see the TextBlock, but there is no Date Time value in it at all. What am I doing wrong?
First of all if you want to update the display time once a second your going to need a timer to trigger an update. A DispatchTimer works works well for that.
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty =
DependencyProperty.Register("TestDateTime", typeof(DateTime), typeof(TestDependency),
new PropertyMetadata(DateTime.Now));
DispatcherTimer timer;
public TestDependency()
{
timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.DataBind, new EventHandler(Callback), Application.Current.Dispatcher);
timer.Start();
}
public DateTime TestDateTime
{
get { return (DateTime)GetValue(TestDateTimeProperty); }
set { SetValue(TestDateTimeProperty, value); }
}
private void Callback(object ignore, EventArgs ex)
{
TestDateTime = DateTime.Now;
}
}
Next we need to modify the XAML so it binds properly to the updated dependency object.
<Window.DataContext>
<local:TestDependency/>
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding TestDateTime}" />
</Grid>
Since we set the DataContext in XAML you can actually delete all of the code behind code in the MainWindow constructor.
If you just want to show some values in your TextBlock, you don't need a Dependency Object here. Try something like this:
public partial class MainWindow : Window
{
public DateTime Test
{ get; set; }
public MainWindow()
{
InitializeComponent();
this.Test = DateTime.Now;
}
}
<Grid>
<TextBlock Text="{Binding Path=Test,RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor}}"></TextBlock>
</Grid>
Here I am not showing the code which can update the value every second. I just want to clarify that this is not the right situation to use Dependency Property.
Of course you can use Dependency Property to do this. But Dependency Object and Dependency Property can offer you some extension functionality such as Data Binding. But it doesn't mean that you need to use a Dependency Object or Dependency Property as the source of the Data Binding.

Binding a Dynamic Application.Resource WPF - User Control

I have a custom class:
class CustomClass
{
public string Test { get; set; }
CustomClass()
{
this.Test = "";
}
}
I'm declaring this custom class on a Application.Resources like that:
<Application.Resources>
<local:CustomClass x:Key="myobj"/>
</Application.Resources>
This resource is the DataContext of a grid and the TextBox binds the Test property, like that:
<Grid DataContext="{DynamicResource myobj}">
<TextBox Text="{Binding Path=Test, Mode=TwoWay}"></TextBox>
</Grid>
Suddenly at run-time, I change the value of the resource
this.Resources["myobj"] = new CustomClass() { Test = "12456" };
I want the value referenced on TextBox be always the value of the object that is currently on "myobj" resource, and I want change automatically the value of the current object when the value of Text property of the TextBox is changed, because of this, I used the Mode=TwoWay, but it's not happening.
I used WPF Inspector and I saw when the resource value is changed, binds a new cleared object and not my created object
I'm new in WPF sorry my english and my unknowledge;
Regards,
EDIT 1
It works implementing the code posted by ethicallogics, thanks! But sorry if I wasn't clear before, when binds a new resource as below
this.Resources["myobj"] = new instance;
it works fine when it is called inside the same window that this resource was declared, unlike when I call this line inside a UserControl, it seems that the UserControl doesn't inherit the MainWindow Resources, how that really works ?
class CustomClass:INotifyPropertyChanged
{
private string _test;
public string Test
{
get
{
return _test;
}
set
{
_test = value;
Notify("Test");
}
}
CustomClass()
{
this.Test = "";
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void Notify(string propName)
{
if(PropertyChanged!=null)
PropertyChanged(this,new PropertyChangedEventArgs(propName);
}
}
Use this class .I hope this will help.

Issue with ToolTip bound to a dependency property

I am facing an issue with ToolTips (again!)..
My code is as follows:
Xaml file:
<Grid>
<Button Height="23" Margin="82,0,120,105" Name="button1" VerticalAlignment="Bottom" ToolTip="{Binding Path=Label, Mode=Default}">Button</Button>
</Grid>
cs file:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
Parameter p1;
System.Timers.Timer aTimer;
public Window1()
{
InitializeComponent();
p1 = new Parameter();
p1.Label = "One thing";
this.DataContext = p1;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Interval = 5000;
aTimer.Enabled = true;
}
void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
aTimer.Enabled = false;
p1.Label = null;
}
}
The Parameter class is as follows:
class Parameter : System.ComponentModel.INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
#endregion
private string label = String.Empty;
public string Label
{
get { return label; }
set
{
label = value;
OnPropertyChanged(new PropertyChangedEventArgs("Label"));
}
}
private void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
{
try
{
if (PropertyChanged != null)
{
PropertyChanged(this, propertyChangedEventArgs);
}
}
catch (Exception exc)
{
}
}
}
Now after the button is clicked, I get the tooltip "One thing" but after 5 seconds, I get an empty tooltip for sometime. Since I set the dependency property to null, I had expected no tooltip.
Afterwards, if I hover the mouse over the button, I get no empty ToolTip (as expected). Its only during the change of value I get the empty ToolTip.
Can you please help.
Try this... change your Label property and _label variable to type object instead of string i.e. use it as string but declare it as an object.
I guess the problem above happens because null string is actually string.Empty and ToolTip is of object type. The boxing that happens assumes null string as string.empty and hence shows a empty tooltip instead of no tooltip. But if Label is object tyupe then null value will represent no ToolTip.
If the suggested data type change of the Label property is not allowed in your case then then use a Converter in the Label binding to return null object for empty string.
Let me know if this helps.

Resources