If I can replace it with a single TextBox (like on a button),
or I can add media element directly to Grid (whatever)...
What is ContentPresenter for? Is there some advantages?
You don't always need a ContentPresenter. It acts as a placeholder that will effectively host any content that you assign to its Content attribute. If you have an area on a given control/page that can hold dynamic content of an indeterminate type, a ContentPresenter is an effective way to hold the space.
It's also used quite a bit with templating, custom controls, etc. Odds are you won't actually use it until you start getting into some fairly advanced stuff.
One kind of cool thing you can do is have the Content attribute of the ContentPresenter bound to a DependencyProperty of type UserControl, and then if you set that DependencyProperty equal to any UserControl (like one that you new up in a ViewModel or something), it'll show up in that spot.
You typically use the ContentPresenter
in the ControlTemplate of a
ContentControl to specify where the
content is to be added. Every
ContentControl type has a
ContentPresenter in its default
ControlTemplate.
From MSDN; so basically it's a placeholder for content in a template.
It is used by ContentControl. Inside ContentControl's template, a ContentPresenter indicates as a placeholder where the actual content will be placed.
From MSDN,
Displays the content of a ContentControl.
Related
This is setting the Content properly explicitly
<Button x:Name ="myButton" Height ="50" Width ="100" Content = "OK">
</Button>
This is implicitly.
<Button x:Name ="myButton" Height ="50" Width ="100">
"OK"
</Button>
I guess this is implicitly because we are not really mentioning Contentby name but how does OK get assigned to Content anyways because a button has many others properties? Why it does't assign to any other property?
While googling, I see implicit/explicit with styles which makes sense but how are they really implicit/explicit in this case? What is the advantage of one over the other if any in this case or difference?
each WPF control has a defined property that gets set when you use it implicitly. In Button's case, it's the Content property. For StackPanel it's the Children property. Usually it's the one property that makes the most sense to be used implicitly.
That's it...
...but how does OK get assigned to Content anyways because a button has many others properties?
The ContentControl class which Button is derived from (through ButtonBase) is decorated with the System.Windows.Markup.ContentPropertyAttribute:
[ContentProperty("Content")]
[DefaultProperty("Content")]
public class ContentControl : Control, IAddChild
...
This attribute indicates which property of a type that is the XAML content property and the XAML processor uses this information to set the property when processing the XAML markup.
In my WPF app, I'm using several ContentPresenters with a special MarkupExtension that requires access to the ContentPresenter's ContentTemplate property.
The MarkupExtension works very well, except that I just found out that if a ContentPresetner uses a ContentTemplateSelector, it doesn't set its own ContentPresenter property: rather, both the ContentPresenter and the result of the ContentTemplateSelector get saved to a private variable of the ContentPresenter class, as can be seen here: link to .Net source code for ContentPresenter.
I figured out that I can call ContentTemplateSelector.SelectTemplate() again and get the template, or keep a dictionary of selected templates inside the ContentTemplateSelector so that I can fetch the template that was geneatedfor each element, but is there a better way to do this?
I have a UserControl without Content, because the control which should be shown inside of the UserControl is created at runtime. I would like to solve this like follows, but don't know how to implement it:
Create a Control-variable in the ViewModel
Set it at runtime when the content is created
Bind a content property (inside the UserControl) to that variable
The problem is, that I don't know how to bind to the control-variable.
Why just not to use ContentControl instead of UserControl and provide Content in runtime by introducing a DataTemplateSelector which able to provide right DataTemplate in runtime?
You can encapsulate your Content-area controls in DataTemplates and select appropriate one in runtime.
I've like 'base' DataTemplate that contains TabControl with 3 tabs. In each tab I put empty ContentPresenter.
I want to write new DataTemplate for each derived type. In which, I want to populate the all 3 ContentPresenter.
How can I reffer to each ContentPresenter of the base template so I can put data inside?
I assume you have set content of each tabitem.
If the content objects have different types you just have to make a datatemplate for each of the types.
Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved?
thanks,
Mark
I'm not sure you'd call this neat but...
Create yourself a new Templated control and call it ImmovableChildWindow.
Modify the class it inherits from to be ChildWindow.
Open Themes/generic.xaml you will find an initial style for the ImmoveableChildWindow
In the Silverlight documentation you'll find the existing template for a ChildWindow at ChildWindow Styles and Templates.
Note the existing TargetType value for the ImmovableChildWindow style.
Copy'n' paste the whole default style for a ChildWindow from the documentation into your themes/generic.xaml file.
Replace TargetType for this copy to the same value as the exiting ImmovaleChildWindow style.
You can now delete the initial style. Leave only the large copy of ChildWindow style now targeting ImmovableChildWindow.
Find within the Template setter change the TargetType of to the same value as the style TargetType
Search through the template and find a Border with the name Chrome. Delete the x:Name="Chrome" attribute. (This is what we are really after).
Now when you create a new ChildWindow item it will by default inherit form ChildWindow, if you want it to be immovable you need modify it to inherit from ImmovableChildWindow instead (change the base type in the code-behind and the root tag name in the xaml).
The ChildWindow attaches events to the FrameWorkElement with the name "Chrome" which enables the child window to be moved about. However being a well-behaved templated control, if it can't find a FrameworkElement called "Chrome" it just continues to work without that feature.
Not Required to Create new class, instead
Copy the style from: http://msdn.microsoft.com/en-us/library/dd833070%28VS.95%29.aspx
Give x:key="stylename"
In Construtor of Childwindow, paste following code before InitializeComponent:
this.Style = App.Current.Resources["childWindow"] as Style;
above solution resolved my issue
Maybe you can try this simple way to do that:
Create a Grid to warp all the content in your ChildWindow.
<Grid Margin="0">
<!--Your ChildWindow. Canvas, Grid, Textblock...Whatever-->
</Grid>
Since the Grid has a 0 margin, you can not click it and move it.