0

Hola buenas noches estoy intentando crear elementos (textbox) en tiempo de ejecución. Pero me salta la excepción a la hora de asignarle la posicion en pantalla (*).PointToScreen:

System.InvalidOperationException: 'Este Visual no está conectado a PresentationSource.'

Alguna Idea por donde van los tiros?, saludos.

Private Sub AñadirCajaEstado(ByVal numeroSector, ByVal estado, ByVal color)
        If Me.Dispatcher.CheckAccess() Then
            Dim textBoxDynamic As New TextBox
            With textBoxDynamic
                .Name = "textBoxDynamic"
                .Text = numeroSector.ToString
                .Height = 20
                .Width = 20
                .TextAlignment = HorizontalAlignment.Center
                .Foreground = Brushes.White
                .Background = color
                .BorderThickness = New Thickness(0)
                If numeroSector <= 15 Then
                    *.PointToScreen(New Point(19 + (20 * Convert.ToInt32(numeroSector.ToString)), 30))*
                ElseIf numeroSector > 15 And numeroSector <= 31 Then
                    .PointToScreen(New Point(19 + (20 * Convert.ToInt32(numeroSector.ToString - 16)), 52))
                Else
                    .PointToScreen(New Point(19 + (20 * Convert.ToInt32(numeroSector.ToString - 32)), 72))
                End If
            End With
            Me.cuadros.Children.Add(textBoxDynamic)
        Else
            Dim d As New añadirCajaEstadoCallback(AddressOf AñadirCajaEstado)
            Me.Dispatcher.Invoke(d, numeroSector, estado, color)
        End If

End Sub

1 respuesta 1

0

he encontrado la solución, y la dejo porque a alguien le puede servir: Como veis, en lugar de asignarle una coordenada, lo inserto dentro de un elemento contedor en este caso el stackpanel.

codigo XAML

<StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="0">
                <StackPanel x:Name="cuadros" Orientation="Horizontal"></StackPanel>
                <StackPanel x:Name="cuadros1" Orientation="Horizontal"></StackPanel>
                <StackPanel x:Name="cuadros2" Orientation="Horizontal"></StackPanel>
            </StackPanel>

codigo .net

 Private Sub AñadirCajaEstado(ByVal numeroSector, ByVal estado, ByVal color)
            If Me.Dispatcher.CheckAccess() Then
                Dim textBoxDynamic As New TextBox
                With textBoxDynamic
                    .Name = "textBoxDynamic"
                    .Text = numeroSector.ToString
                    .Height = 20
                    .Width = 20
                    .TextAlignment = HorizontalAlignment.Center
                    .Foreground = Brushes.White
                    .Background = color
                    .BorderThickness = New Thickness(0)
                    If numeroSector <= 15 Then
                        Me.cuadros.Children.Add(textBoxDynamic)
                    ElseIf numeroSector > 15 And numeroSector <= 31 Then
                        Me.cuadros1.Children.Add(textBoxDynamic)
                    Else
                        Me.cuadros2.Children.Add(textBoxDynamic)
                    End If
                End With
    
            Else
                Dim d As New añadirCajaEstadoCallback(AddressOf AñadirCajaEstado)
                Me.Dispatcher.Invoke(d, numeroSector, estado, color)
            End If
        End Sub

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.