How can I create and use a GridLengthConverter Object ?

Platform: WPF| Category: GridSplitter

The following example shows how to create and use an ‘instance’ of the GridLengthConverter object. The example defines a custom method called ‘changeCol()’, which passes the ListBoxItem to a GridLengthConverter that converts the Content of a ListBoxItem to an instance of GridLength. The converted value is then passed back as the value of the ‘Width’ property of the ColumnDefinition element.

[C#]
 
public void changeColVal(object sender, RoutedEventArgs e) 
        {
            txt1.Text = 'Current Grid Column is ' + hs1.Value.ToString();
        }

        public 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;
            }
        }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.