Compartir a través de


Cómo: Crear y utilizar un objeto GridLengthConverter

Actualización: noviembre 2007

Ejemplo

En el ejemplo siguiente se muestra cómo crear y utilizar una instancia de GridLengthConverter. En el ejemplo se define un método personalizado denominado changeCol, que pasa el control ListBoxItem a un objeto GridLengthConverter que convierte la propiedad Content de ListBoxItem en una instancia de GridLength. A continuación, el valor convertido se devuelve como valor de la propiedad Width del elemento ColumnDefinition.

En el ejemplo también se define un segundo método personalizado, denominado changeColVal. Este método personalizado convierte la propiedad Value de Slider en un valor String y, a continuación, devuelve el valor al objeto ColumnDefinition como propiedad Width del elemento.

Observe que un archivo Lenguaje de marcado de aplicaciones extensible (XAML) independiente define el contenido del objeto ListBoxItem.

Para ver el ejemplo completo, incluido el archivo XAML relacionado, vea Ejemplo GridLengthConverter Object.

Private Sub changeColVal(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))

    txt1.Text = "Current Grid Column is " + hs1.Value.ToString()
End Sub

Private Sub changeCol(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim li1 As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myGridLengthConverter As System.Windows.GridLengthConverter = New System.Windows.GridLengthConverter()
    If (hs1.Value = 0) Then

        Dim gl1 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col1.Width = gl1

    ElseIf (hs1.Value = 1) Then

        Dim gl2 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col2.Width = gl2

    ElseIf (hs1.Value = 2) Then

        Dim gl3 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col3.Width = gl3
    End If
End Sub
        private void changeColVal(object sender, RoutedEventArgs e) 
        {
            txt1.Text = "Current Grid Column is " + hs1.Value.ToString();
        }

        private void changeCol(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            GridLengthConverter myGridLengthConverter = new GridLengthConverter();
            if (hs1.Value == 0)
            {
                GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col1.Width = gl1;
            }
            else if (hs1.Value == 1)
            {
                GridLength gl2 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col2.Width = gl2;
            }
            else if (hs1.Value == 2)
            {
                GridLength gl3 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col3.Width = gl3;
            }
        }

Vea también

Referencia

GridLengthConverter

GridLength