Wpf Contentcontrol Binding Not Updating
ContentControl and ContentPresenter do not propagate their DataContext to their Content On 11:54 In programming, WPF/Silverlight → 2 comments A quick post here about using a ContentPresenter (or a ContentControl which uses a ContentPresenter in its template) with its Content property. WPF: The real difference between ContentControl and ContentPresenter. It looks the same as the “Binding ABorderProperty”, but it’s not).
I've created a control with 3 PART_s, one PART_ changes depending on the type bound to it, however values changed within the Control do not update the Binding, it seems to work as OneWay Binding.
- Responding to changes. You will obviously be binding to your own data objects. This is just as easy, but once you start doing it, you might discover something that disappoints you: Changes are not automatically reflected, like they were in previous examples. You need just a bit of extra work for this to happen, but fortunately, WPF.
- Given a binding path expression in which every element is a DependencyProperty and the final property is a view bound to a ContentControl, why does a change in a property in the middle of the path not cause the binding to update?
- Value set from the datatrigger not updating binding source. WPF) Windows Presentation Foundation. ContentControl Content.
- [c]Binding annotations with MVVM Observable collection not updating after OnLoad Welcome to the SciChart Community Forums! Please use the forums below to ask questions about SciChart.
Here's part of the code I beleive is relevant:
..
For Content I've also tried .. RelativeSource={RelativeSource AncestorType=local:DABaseControl}
but no change.
And now, just click continue profiling, and here you go, you have the ANTS Memory Profiler working without license. Windowblinds 7.4 crack. So what to do? What you have to do is to close that dialog using the X from the right upper corner - see the screenshot. No need to do that anymore because there is an easy way to use it without having a license, crack or keygen (Note: the version that I am using is 7.1). Start the ANTS Memory Profiler, the ANTS Memory Profiler Free Trial Window will appear, as in screenshot below.
If the DataTemplate Binding use '{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}'
the template doesn't change once set.
Or is there a better way to do this?
Thanks
cpx2 Answers
I just encountered the same problem, I wanted to create a DataTemplate
with DataType='{x:Type sys:Boolean}
that just had a checkbox. But there were many warning signs along the way telling me this isn't the way it should be done.
At first, the simple binding of {Binding}
would throw an exception 'Two-way binding requires path or xpath', which was the first warning sign. I changed the binding to {Binding .}
which worked (even though this MSDN article clearly states that they're equivalent). That fact that voodoo was helping was the second warning sign. It then displayed correctly and the checked state was according to the boolean value, but when clicking the checkbox (even with UpdateSourceTrigger=PropertyChanged
), it refused to update the binding source, no matter what I tried. Using diagnostics:PresentationTraceSources.TraceLevel=High
showed that it didn't even try to bind back (third warning sign).
I went ahead and created a simple 'box' for the bool value - a class with a single bool property named Value
with anINotifyPropertyChanged
implementation. I changed the binding to {Binding Value}
and now everything worked, including two way binding.
Conclusion: It seems a binding can't update the bound object itself, but only properties of that object (which is why {Binding}
throws an exception, but the more explicit {Binding .}
suppresses that exception, according to H.B.'s answer). In any case, the approach of creating a ViewModel and creating templates that target it appears to be more than a mere design guideline, but an actual technical requirement.
I've actually never worked with a ContentTemplateSelector
, but if I had to hazard a guess I would say either it's not responding to PropertyChanged
events on your ContentControl.Content
property, or your Content
binding is incorrect.
You can easily check if your binding is correct or not by removing the ContentTemplateSelector
and seeing if data shows up at all. If it does, your binding is correct. If it doesn't, it's incorrect and you need to fix it.
If the problem is the ContentTemplateSelector
, then I would suggest switching to a DataTrigger
which determines which ContentTemplate
to use based on the Content. This is what I usually do, and it uses a Converter which simply returns typeof(value)
Not the answer you're looking for? Browse other questions tagged c#wpfxamlbinding or ask your own question.
Sometime in the past, a friend asked me how to update a control to show status while his code is doing a loop of stuff. Essentially changing the text of a label (or sophisticatedly we can say a text-based progress bar). In my past coding with MFC and WinForms, it's fairly easy enough, you just invalidate and do an update (Invalidate / UpdateWindow in MFC or Invalidate / Update in WinForms). This approach also coincides with how Windows UI operate, where you specify the region that needs to be redrawn and then you send a message to the message pump for that control to paint itself.
So, I expected something similar (if not exactly the same) to also be present in WPF; much to my surprise, there is no equivalent. All my internet searches actually shows how to do this using background thread - it is the approach that needs to be taken in a proper programming context, however there are times when you just want to do something quick & dirty or you want to augment an existing app / port where you don't want to introduce new elements. There are also considerations to be made when both UI and worker thread access the same data, especially with regard to data binding (see my post about collection change not supporting multi-threading out of the box).
So, I've decided to add a helper method to refresh a WPF control. I really appreciated the Refresh method in WinForms (which executes both Invalidate & Update), so I'm renaming my method to be Refresh as well. The code snippet below also show some C# specific techniques, namely: anonymous delegates and extension methods.
publicstaticclassExtensionMethods privatestaticAction EmptyDelegate = delegate() { }; publicstaticvoid Refresh(thisUIElement uiElement)Elaborare il 2 tempi facchinelli pdf download. uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } privatevoid LoopingMethod() for (int i = 0; i < 10; i++) label1.Content = i.ToString(); Thread.Sleep(500); } |
The LoopingMethod is just the method I use in my Window class to update the label (updating the progress) and then the code does some heavy lifting (Sleep ). The Refresh method is the extension method that takes any UI element and then calls that UIElement's Dispatcher's Invoke method. The trick is to call the Invoke method with DispatcherPriority of Render or lower. Since we don't want to do anything, I created an empty delegate. So how come this achieves refresh functionality?
When the DispatcherPriority is set to Render (or lower), the code will then execute all operations that are of that priority or higher. In the example, the code already sets label1.Content to something else, which will result in a render operation. So by calling Dispatcher.Invoke, the code essentially asks the system to execute all operations that are Render or higher priority, thus the control will then render itself (drawing the new content). Afterwards, it will then execute the provided delegate (which is our empty method).
Pretty weird; there was a post somewhere in my google search that led me this route, and I was surprised as to how it worked. I couldn't find it anymore, but credit where credit is due, someone else figured out that Invoke-ing a Render or lower priority task will result in the UI being redrawn.
Update (January 20, 2009):
A Commenter asked for a full sample, so I've uploaded one here. I don't speak Spanish, but Google translator was working great!
Update (February 26, 2009):
A Commenter asked for a VB.NET sample, so I've uploaded one here.
Update (January 13, 2010):
Apparently geekswithblogs doesn't allow linking non-image files (thus the samples link doesn't work anymore). Samples are now being put in my Google Sites site. C# sample here and VB.NET sample here.
Posted on Monday, August 25, 2008 10:22 AM WPF , .NET Back to top