serhio
serhio

Reputation: 28586

WPF: Invisible Control (?)

I try to understand, why the WPF Control, or derived from control custom controls aren't visible ever:

Say, we have the following:

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication13"
        Title="MainWindow" Height="350" Width="525">

    <DockPanel Name="dockPanel1" Background="LightBlue">

        <Canvas DockPanel.Dock="Left" ToolTip="tt one" Width="200" Background="Blue">
            <Control Background="Red" ToolTip="tt control" BorderBrush="Red" BorderThickness="5" Width="50" Height="100"></Control>
        </Canvas>

        <Button Content="two" DockPanel.Dock="Left" ToolTip="tt two" ></Button>
        <!--<my:MyControl Background="Red" ToolTip="tt hello" BorderBrush="Red" BorderThickness="5"></my:MyControl>-->

        <Control Background="Red" ToolTip="tt hello" BorderBrush="Red" BorderThickness="5"></Control>
    </DockPanel>
</Window>

Any presence of the indicated Controls - any border, any tooltip, any background is not detected.

Why this, and how to make the controls visible?

Upvotes: 2

Views: 1545

Answers (3)

serhio
serhio

Reputation: 28586

Found some explanation from MSDN:

A Control that does not have a ControlTemplate is not visible in your application, and setting the following properties has no effect unless the ControlTemplate references them explicitly:

  • Background
  • BorderBrush
  • BorderThickness
  • FontFamily
  • FontSize
  • FontStretch
  • FontWeight
  • Foreground
  • HorizontalContentAlignment
  • VerticalContentAlignment

A common way to use these properties is to bind an element in the ControlTemplate to the property. For example, if you want your control to change color according to the value of the Background property, you can bind some property of an element in the ControlTemplate to the Background. Use the TemplateBinding Markup Extension to bind properties on a control to an element in the ControlTemplate.

Upvotes: 1

Bruno
Bruno

Reputation: 1974

I think that delaring a control like that in XAML does not mean anything since is does not represent something visual on itself.

You need to define a template for that control and for the controls inside, bind their property with TemplateBinding.

something like that :

        <Canvas DockPanel.Dock="Left" ToolTip="tt one" Width="200" Background="Blue">
        <Control Background="Red" ToolTip="tt control" BorderBrush="Red" BorderThickness="5" Width="50" Height="100">
            <Control.Template>
                <ControlTemplate>
                    <TextBlock Text="Hello" Background="{TemplateBinding Background}"/>
                </ControlTemplate>
            </Control.Template>
        </Control>
    </Canvas>

Upvotes: 2

Thomas Levesque
Thomas Levesque

Reputation: 292695

The Control class is the base class for controls that can be templated, but it doesn't define a template itself. And a control with no template has no visual tree, so it doesn't show up.

Upvotes: 4

Related Questions