VisualBrush at Runtime BUG?

Hi,

I'm filling a visualbrush using a custom control at runtime. I have something like this in my main window:

<object>

<DiffuseMaterial>
<
DiffuseMaterial.Brush>
<
VisualBrush x:Name="visualBrushCustom"></VisualBrush>
</
DiffuseMaterial.Brush>
</
DiffuseMaterial>

In the code behind:

void Window1_Loaded(object sender, RoutedEventArgs e)
{
customControl =
new CustomControl();
visualBrushCustom.Visual = customControl ;
.....

BUT The customcontrol is not rendered as visualBRush. If I insert the customcontrol inside my window and then redirect the visualbrush to that control it works.

Is it normal Do you know a workaround

Thanks

Giorgio





Answer this question

VisualBrush at Runtime BUG?

  • Raananan

    I had to call

    myPerspectiveCamera.InvalidateArrange();

    for it to show up when the window loads



  • Devil_Soul

    try this

    VisualBrush vb = FindName("visualBrushCustom") as VisualBrush;

    vb.Visual= cc;



  • Azzmodan

    Of course I can...

    This is the xaml:

    <Viewport3D Width="800" Height="600" Name="myViewport" Focusable="True" MouseLeftButtonDown="HitTest">

    <Viewport3D.Camera>
    <PerspectiveCamera x:Name="myPerspectiveCamera"
    FarPlaneDistance="15" LookDirection="0,0,1" UpDirection="0,1,0" NearPlaneDistance="1" Position="0,0,-3" FieldOfView="50" />
    </Viewport3D.Camera>

    <ModelVisual3D x:Name="topModelVisual3D">

    <ModelVisual3D>
    <ModelVisual3D.Content>
    <AmbientLight Color="#333333" />
    </ModelVisual3D.Content>
    </ModelVisual3D>
    <ModelVisual3D>
    <ModelVisual3D.Content>
    <DirectionalLight Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
    </ModelVisual3D.Content>
    </ModelVisual3D>
    <ModelVisual3D x:Name="side1ModelVisual">
    <ModelVisual3D.Content>
    <GeometryModel3D x:Name="side1GeometryModel3D" Geometry="{StaticResource CubeSide01}" >
    <GeometryModel3D.Material>
    <DiffuseMaterial>
    <DiffuseMaterial.Brush>
    <VisualBrush x:Name="visualBrushCustom"></VisualBrush>
    </DiffuseMaterial.Brush>
    </DiffuseMaterial>
    </GeometryModel3D.Material>
    </GeometryModel3D>
    </ModelVisual3D.Content>
    </ModelVisual3D>

    </ModelVisual3D>

    </Viewport3D>

    This is the codebehind:

    public partial class Window1 : Window
    {

    CustomControl customControl ;

    public Window1()
    {
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
    customControl = new CustomControl();
    visualBrushCustom.Visual = customControl ;
    }
    }

    And this is the code of CustomControl:

    <Grid x:Class="Prova3D.CustomControl"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    >

    <TextBox Width="200" Height="50" Name="txtX"></TextBox>

    <Button Width="50" Height="50" Name="btnOne"></Button>

    </Grid>

    Hope now it's cleaner...
    Thanks for your help


  • thenowayman

    I'm sorry,

    but this is not the answer to my question....


  • blackcatsbones

    can you post a little bit more code so the context is more clearer

  • Jimmy M Joy

    it draws the customcontrol, but for some reason it is visible only after you try to resize window

  • praveench2k

    Thanks! That really solved the problem!! I used:

    myViewport.InvalidateArrange();

    Thank you very much Lee d.

    Giorgio


  • PieterE.care

    if visualbrush is a resource, you have to find the resource by the name and then set the visual. it does work correctly

  • VisualBrush at Runtime BUG?