Melursus
Melursus

Reputation: 10578

Display a VisualBrush from a hidden control

I got the following xaml

<Grid>
<Grid.RowDefinitions>
  <RowDefinition Height="Auto" />
  <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBlock x:Name="_sampleText"
           Grid.Row="0"
           VerticalAlignment="Stretch" 
           HorizontalAlignment="Stretch"
           Width="200" 
           Height="50"
           FontSize="36"
           Text="Hello world"
           TextAlignment="Center"
           Visibility="Collapsed" />

<Border Grid.Row="1" 
        Width="{Binding ActualWidth, ElementName=_sampleText}" 
        Height="{Binding ActualHeight, ElementName=_sampleText}">
  <Border.Background>
    <VisualBrush Stretch="None" 
                 Visual="{Binding ElementName=_sampleText}" />
  </Border.Background>
</Border>
</Grid>

I want my visual to be render even if the control he bind too is not visible. Is there a way of doing that?

Upvotes: 1

Views: 1880

Answers (2)

Dennis
Dennis

Reputation: 20561

No. If the Visibility=Collapsed then it won't take part in the measure/arrange layout pass and not be rendered (the Width and Height will be 0).

What effect are you trying to achieve? It looks like you want a preview pane? Reply in the comments and we can figure out the best approach.

Edit: There is a way to call the measure/update on a UIElement from code-behind however I think the best-approach for you would be use a BoolToVisibilityConverter.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <CheckBox x:Name="OptionCheckBox" Grid.Row="0" />
    <Border Grid.Row="1" Visibility="{Binding Path=IsChecked, ElementName=OptionCheckBox", Converter={StaticResouce BoolToVisibilityConverter}}"> 
        <TextBlock Width="200" Height="50" FontSize="36" Text="Hello world" TextAlignment="Center"/>
    </Border>
</Grid>

Upvotes: 0

Clemens
Clemens

Reputation: 128061

You could also place the TextBlock in a Border and make that Hidden:

<Border Visibility="Hidden">
    <TextBlock ... />
</Border>

Upvotes: 2

Related Questions