ContentPresenter Not Always Working in Silverlight Template - xaml

When we customize certain controls through a ControlTemplate, the ContentPresenter does not render the control as it should. Why is this?

It would really be easier if you let me see the affected xaml markup. My idea on what might be the problem is based on the rather dirty default behavior of the ContentPresenter: when it is used inside the ControlTemplate of a ContentControl, it will automagically bind to the Content and ContentTemplate properties. But it won't do this for any other control type. And therefore has to be done explicitly.
Do you have those bindings set:
<ControlTemplate ...>
...
<ContentPresenter Content="{TemplateBinding ...}"
ContentTemplate="{TemplateBinding ...}" />
...
</ControlTemplate>

Related

How do I edit the header of an Expander control in WinUI 3?

When I edit properties of the Expander control, the effects seem to only apply to the content of the Expander, rather than the header.
E.g. if I set the BorderBrush as black and the Background as aquamarine, those properties only apply to the content:
<Expander Header="Expander" BorderBrush="Black" Background="Aquamarine">
<TextBlock Text="Here is some text"/>
</Expander>
See what the above code looks like
I know I can use this syntax:
<Expander>
<Expander.Header>
<!--XAML content here-->
</Expander.Header>
</Expander>
and put XAML content inside the header. So I tried putting a StackPanel in there, and editing the border and background on that—but it only applied to a small portion of the Expander header, and didn't cover the drop down caret, for example.
How do I change properties of the header?
You can edit the default style but this might be easier:
<Border
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="SkyBlue"
BorderBrush="HotPink"
BorderThickness="1"
CornerRadius="{StaticResource ControlCornerRadius}">
<Expander
Content="CONTENT"
Header="HEADER" />
</Border>
You just need to set the Border's properties.

TemplateBinding in Xamarin.Froms not working

I tried this simple example from the official Xamarin page, that contains a template binding like:
<ControlTemplate x:Key="TealTemplate">
<Grid>
<Label Text="{TemplateBinding Parent.HeaderText}" />
<Label Text="{TemplateBinding Parent.FooterText}" />
</Grid>
</ControlTemplate>
However, i always get ths error by just copying the example code to Visual Studio.:
Can anyone give me a short example how to bind from within a ControlTemplate to a property of the templated control, be it of the binding context or the control itself?
I haven't worked out the issue with 'Parent'. But for my project I use:
{TemplateBinding BindingContext.IsBusy}
IN the page that use ControlTemplate, I also use a ViewModel and binding this ViewModel, which has IsBusy property, to the page.

UWP Why does a style not apply to TargetTypes in DataTemplate?

Given a style in a Page.Resource:
<Style x:Name="ItemTitle" TargetType="TextBlock">
<Setter Property="FontSize" Value="16"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
It is correctly applied to any regular TextBlock on the same page.
However, when I use a DataTemplate for an Item in a GridView on that page, this style does not apply.
<DataTemplate x:Key="Output" x:DataType="vm:Output">
<TextBlock Text="{x:Bind Text}"></TextBlock>
</DataTemplate>
It does work when I apply the style explicitly on the DataTemplate, e.g.:
<DataTemplate x:Key="Output" x:DataType="vm:Output">
<TextBlock Style="{StaticResource ItemTitle}" Text="{x:Bind Text}"></TextBlock>
</DataTemplate>
Does anyone know what's up?
It's expected and intentional. If it doesn't derive from Control (like DataTemplate) then it won't inherit an implicit style unless they're in the application resource dictionaries as global defaults.
Or more specifically;
Templates are viewed as an encapsulation boundary when looking up an implicit style for an element which is not a subtype of Control.
Hope this helps. Cheers.
Addendum:
If it's a situation where you have a lot of the same element nested in a Template you can just set it once and allow it to inherit to all the nested controls of the type like (in pseudo);
<Parent>
<Parent.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource ItemTitle}"/>
<Parent.Resources>
<!-- These will all inherit the Style resource now,
without explicit style setting individually. -->
<TextBlock/>
<TextBlock/>
<TextBlock/>
</Parent>

Forward all properties of a control

I'm making a custom control (that's what I do, I'm a theme designer) and I'm having trouble with something.
The control I'm making is a custom RichTextBox control. Since I want a custom border (rounded edges) arround the RichTextBox, I simply have a control with custom borders, and a child control which is actually a normal RichTextBox. With the BorderStyle set to none, it looks like this:
Now, of course, the parentcontrol needs to have all the properties a normal RichTextbox has.
I manually did all the properties for a regular textbox, but a RichTextBox has far more properties and I was asking myself, is there any way to "forward" all properties of the child control to the main control?
Maybe I'm explaining it a bit oddly. Basically, when you change the main control's "Text" property the RichTextBox's Text property should change too. This means I have to write a custom event for every Property a RichTextBox has, isn't there a way to do this for every property automatically?
Thanks in advance,
Mavamaarten.
Turns out there is no way to do this.
Note: I answered this before the posted had the WinForms tag in their question. (I actually added it based on his reply here.) Still, if you're using WPF, this is how you'd do it...
The easiest thing to do is to replace the ControlTemplate with your internal implementation. In other words, you're replacing the 'visual' portions of the RichTextBox control but you're still a RichTextBox control.
For instance, this is how I replaced the visuals of a TextBox to completely strip away all the chrome and leave it with nothing more but a simple border. But it was still a full TextBox with all properties, etc.
Again, look up Control Templating. You may also want to search for 'Lookless Controls'.
<Style TargetType="{x:Type glc:EditableTextBlock2}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="MinWidth" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type glc:EditableTextBlock2}">
<Border Name="Bd"
SnapsToDevicePixels="True"
BorderThickness="1"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Border.Background}" >
<ScrollViewer Name="PART_ContentHost"
SnapsToDevicePixels="True"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How to disable ListView's Hover and Tile effects?

I want to disable Tile effect that is some kind of pushed effect and hover background color effect of ListView control, how can i do that?
Thanks
After some googling I found that the highlighting happens in the ListViewItemPresenter, which turns out to be pretty hidden. It's located inside the ControlTemplate of an ListViewItem, which is the ItemContainer for the ListView. The simplest way I've found to disable the effect is to simply override this ControlTemplate:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<TextBlock Text="List Item" />
...
<TextBlock Text="List Item" />
source: https://blog.jonstodle.com/uwp-listview-without-highlighting-and-stuff/
Look at this question:
Disable cross-slide selection for a listview
You can also make changes to the template to remove any visual states and adornments - go to the designer and right click your ListView/Edit Additional Templates/Edit Generated Item Container (ItemContainerStyle)/Edit a Copy... - that will extract the template you can modify using your preferred method.