i wonder if there is a way to access a control's templatepart from within c# for modifying the part (e.g. hiding, etc..). is it possible to get a reference to the part with pure c#?
i don't want to touch the controls template.
thanks
j.
It is possible, but its quite nasty.
On the Template there is a method called FindName, which needs two arguments: the name and the FrameworkElement that has the ControlTemplate as Template. Of course, you need to set the name of the element in the ControlTemplate...
Another more elegent solution is to use a Binding in the ControlTemplate to determine the visibility.. That way you do not need to do stuff in your code behind and you can keep it Xaml only...
Related
Is there a way to use the same style/template for two controls but with a modified property without rewriting the whole code? For example two ScrollViewers with the same style or template but with a different background.
It's related to my other question: Change property of component from template based on parent
I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)
Well, i must admit, still sometimes XAML seems a bit mysterious to me. The thing is, i always liked to debug through the C# code (setting lots of breakpoints in them) to get the idea of "what is happening" and "how is it happening". But with declarative XAML syntax that's not an option. I think you'll agree that to work with XAML, or to be precise, to work with/understand some existing XAML code you got to "already know" how things work with XAML declaration. There is just no way you can know/learn things investigating the execution of your application code. So i'm more than interested to take a look through XAML inside-out, as detailed as possible. I'm NOT talking about "learning" XAML, I know the basic stuff. May be i can provide some examples to clarify the sort of things i'm looking for -
Compared to C# code how an object gets instantiated when we use them in XMAL? Are they stored in managed heap? Same way as C# code-instantiated objects?
How the properties get set while using Mark-Up Extension syntax for Data/Command Binding?
When any property of an INotifyPropertyChanged type gets updated, how the Binding instatnce inside the XAML syntax updates the itself? How exactly it gets notified it at the first place, & by whom?
A viewmodel can be set as the DataContext of a view at runtime by defining Typed DataTemplate, like -
<DataTemplate DataType="{x:Type viewmodels:AccountsViewModel}">
<views:Accounts/>
</DataTemplate>
How does it happen actually? What are the rules for setting DataContext other than searching for the DataContext property upward the logical tree?
How the whole template things (DataTemplate, ControlTemplate & ItemsPanelTemplate) are treated/resolved at run time.
etc. etc. etc.
So if you are good/experienced/expert in XAML what would you suggest (links, articles, blogposts, books whatever) as reference that helps getting clear & deeper understanding about how XAML works "under-the-hood"? Thanks in advance.
Most can be explained by don't thinking of XAML as a real programming language, more like a declarative language. Everything you do in xaml, can be made in C# aswell, and in fact this is whats happening.
Compared to C# code how an object gets instantiated when we use them
in XMAL? Are they stored in managed heap? Same way as C#
code-instantiated objects?
Yes because they are just c# objects. Most resources are stored in a hibernated state, i rememberd the word inflated somewhere. Converter or other "direct" c# objects are created when they are needed. Important here is that these resources are usually shared, so they will get created only once.
How the properties get set while using Mark-Up Extension syntax for Data/Command Binding?
This again depends on where you use the markup extension. In a Style? In a Template? In a instanced user control like a window? Usually they are evaluated when you actually need them. It wouldn't make sense to evaluate them, when the inflated style is stored in the actual resource dictionary. They get evaluated when you actually use the style on an object.
When any property of an INotifyPropertyChanged type gets updated, how
the Binding instatnce inside the XAML syntax updates the itself? How
exactly it gets notified it at the first place, & by whom?
By the binding engine. WPF checks if your DataContext inherits the INotifyPropertyChanged interface, attaches to the event provided by the interface and listens to any changes. If such an event is raised, the binding engine will just call the getter again.
How does it happen actually? What are the rules for setting DataContext
other than searching for the DataContext property upward
the logical tree?
In short: None other. Datacontext is simply an inherited attached property. If you don't re set it on a child control, it will take the value the parent has until it reached the root. The only exception to this are ContentControls and ContentPresenter they will not inherit the DataContext but will change them depending on the content. So these controls always have by default the Content as their DataContext.
How the whole template things (DataTemplate, ControlTemplate & ItemsPanelTemplate) are treated/resolved at run time.
Simply spoken: Everytime WPF finds a non ui object, it tries to find a DataTemplate for the given type. In an ItemsControl for example: You can bind a list of MyClass; unless you provide an explicit DataTemplate or DataTemplateSelector it will search the resource tree upwards for an implicit style. Again remember that this already does not happen in XAML, but on the C# objects that was generated out of the xaml.
And is it by any means possible (at present or near future) to debug
through XAML code?
How do you think you can debug something that is not executed, but evaluated on compile time?
Please don't take this as 100% correct. Over the Years this is what i gathered of informations about XAML and the usage. If you have any corrections or find something that is clearly wrong. Please tell me, we are all here to learn and i always learn new things about the stuff i use :)
I'm attempting to use a DataTemplateSelector with a particular third-party WPF grid control, and I'm having trouble determining if the issues I'm having are a bug in the control or my own lack of understanding about the conventions of WPF data templates.
I realize that the ordinary use case of a DataTemplate is to declare it somewhere in XAML (be it as a resource or explicitly where it's used), but my particular project would benefit greatly if I could create the template in code (C#, specifically) rather than in XAML. The issue I'm running into is the fact that my code-created DataTemplate uses a FrameworkElementFactory as the template's VisualTree, whereas a XAML-created template uses a TemplateContent object as the template's Template value. As best I can tell right now, the grid control in question works with templates that use Template, but doesn't seem to play nicely with templates that use VisualTree.
By way of comparison, here's what one of the templates looks like in XAML as part of my selector:
<MySelectorType>
<MySelectorType.BooleanTemplate>
<DataTemplate>
<EditorControl Name="Reserved_Name" />
</DataTemplate>
</MySelectorType.BooleanTemplate>
</MySelectorType>
And here's how I'm trying to create an equivalent template in code:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
{
Name = "Reserved_Name"
}
};
I've also tried it like this:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
};
template.VisualTree.SetValue(EditorControl.NameProperty, "Reserved_Name");
Which seemed more analogous to what the XAML template would do, but that appeared not to work at all (the editor neither read or set the value, where at least the first version would read it).
Is it possible for my in-code template to use the Template property rather than VisualTree? According to the documentation, there's no public API for this type and the instantiation path is complex, but has this been done? The only example I've found uses hardcoded XAML in the code, which doesn't sit well with me.
I do not like this way of doing things either but this actually is the recommended way, in the documentation of the FrameworkElementFactory the following can be found:
This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.
I do not know of any simple way to use the Template property in code, the only way to my knowledge that might be possible is via a lot of reflection.
Setting names is a special case, if you set the Name property of the factory it should be properly registered, if not you need to get the approriate Namescope and register the name manually.
I created a custom control in Expression Blend which consists of multiple TextBlocks placed in a Grid. Now I added this custom control to my phone page in Visual Studio and want to access and change the text of these TextBlocks from C# code.
How do I access these sub-controls in code?
I thought I could do something like this:
MyCustomControl.TextBlock1.Text = "New Text";
But it's not that easy. So how do I do it?
The property MyCustomControl.TextBlock1 exists but is internal, not public. You can use MyCustomControl.FindName("TextBlock1") as TextBlock to locate the resources by name instead.
Does GetTemplateChild(string name); work ? You should be able to use it, to access the elements of your control's template
Try below code both should work for your requirement:
Control subControl1 = (Control)MyCustomControl.Controls[0];
or
TextBox subControl1 = (TextBox)MyCustomControl.Controls[0];
Using any code you are able to access Text Property.
Always keep remember the hierarchy in your custom control and then try to access all control level-by-level.
If still you are facing any issue feel free to ask.