So based on your comment I would suspect that the binding does not work because there is no DataContext in the place where you try to bind.
Up vote 0 down vote favorite share g+ share fb share tw.
I have a UserControl which is used as the basis for items in an ItemsControl: Main Page xaml: I am trying to set the 'System' property of each ViewModel (such that it can handle the data for the view), but the property is never set! Here is the dependancy property declaration in the view model class: public static readonly DependencyProperty SystemProperty = DependencyProperty. Register( "System", typeof(ServerGroup), typeof(ServerGroupControlViewModel) ); public ServerGroup System { get { return (ServerGroup)GetValue(SystemProperty); } set { SetValue(SystemProperty, value); } } The property always keeps it's default value.
Any ideas on why this setup does not work? C# wpf xaml mvvm link|improve this question edited Jan 30 at 3:34H.B.43k61645 asked Jan 30 at 3:29Andy1256.
If you set a breakpoint in that setter you will always be disappointed as the binding system does not use them. – H.B. Jan 30 at 3:38 @H. B I'm inspecting the view later (breaking in the handler of a command which uses the property).
I have indeed read about how the setter is skipped as per wpf's magic. – Andy Jan 30 at 3:49 Alright, another unknown: Does the binding actually work without throwing binding errrors? That is what I suspect here.
– H.B. Jan 30 at 3:51 Yes, no binding errors thrown unless I try Mode=TwoWay (as was suggested by a previous answer that disappeared). The error from Mode=TwoWay specifies that 'Path or XPath' must be set, i.e. Can't two way bind to the DataContext of the DataTemplate which makes sense.
– Andy Jan 30 at 3:55.
So based on your comment I would suspect that the binding does not work because there is no DataContext in the place where you try to bind. Your VM is not a FrameworkElement so it has no DataContext property, presumably it is not Freezable either (and hence might lack an inheritance context too) so I suspect that this won't work. (ElementName and RelativeSource won't work either then, by the way) I suggest you approach this differently, also I do not recommend the use of DPs in VMs due to thread-affinity and other problems.
Here's one gem of a work-around: Yeah, please don't do that...
Giving up on current approach - seemingly impossible. Thanks for the help. – Andy Jan 30 at 4:17 @Andy: You're welcome, been constructing a horrifying workaround for fun if you are interested.
– H.B. Jan 30 at 4:20 Removes the binding error but property is still not set ;) – Andy Jan 30 at 4:28 @Andy: Really? Well, I obviously could not test this with your specific control, so it might not work in every context... – H.B. Jan 30 at 4:30.
When a property is set through databinding, the explicit setter function is not called. If you really want to intercept the setting action, you can do this via a callback that you set when initializing the Dependency Property. Public static readonly DependencyProperty SystemProperty = DependencyProperty.
Register( "System", typeof(ServerGroup), typeof(ServerGroupControlViewModel), new PropertyMetadata { PropertyChangedCallback = SystemPropertyChanged } ); static void SystemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ServerGroupControlViewModel receiver = (ServerGroupControlViewModel)d; ServerGroup newValue = e. NewValue as ServerGroup; // Add any appropriate handling logic here. }.
I have previously attempted to add the PropertyChangedCallback to the property metadata and wait for it to be called. It is never called (breakpoint never hit)! – Andy Jan 30 at 3:52 @Andy - in your previous attempt, was the value applied explicitly or via binding?
If you set the value explicitly then (I'm 90% sure that) it will use your property setter function, but if you use binding than that callback will fire. – Andrew Shepherd Jan 30 at 3:54 Via the binding (as per the supplied snippet). Have not tried setting it explicitly yet.
– Andy Jan 30 at 3:57.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.