Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

DataGrid - determine usercontrol I want in my DataGridT... RSS

1 reply

Last post Apr 23, 2008 07:00 PM by JKewley

(0)
  • JKewley

    JKewley

    Member

    14 Points

    3 Posts

    DataGrid - determine usercontrol I want in my DataGridTemplateColumn when binding

    Apr 23, 2008 02:49 AM | LINK

    I'm trying to load a user control into a TemplateColumn at run time. The scenario is such that the first column in my grid will display a green indicator for success and a red indicator for failure. It's a little more complicated than that, so I'm not looking for a solution that uses an IValueConverter to bind the background color of a control to a value.

    Instead, what I want to do is use an IValueConverter to insert a user control into the template column. My thought was to use binding against a boolean field in my data source, and return a GreenIndicator user control when true, and a RedIndicator user control when false. My issue is how to hook the binding into an entity that can host my user control.

    Another approach might be to use traditional databinding events, but I haven't had much success finding examples of that. Boilerplate pseudocode follows:

    <data:DataGrid.Columns>
     
    <data:DataGridTemplateColumn Width="50">
       
    <data:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <UserControl Type="{Binding IsOn, Converter={StaticResource LightingImageConverter}}"/>
          </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>
    ........

    public class LightingImageConverter : IValueConverter{
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var success = (bool)value;
            if (success) return new GreenIndicatorControl();
            else return new RedIndicatorControl();
        }
        ......
    }

    Thanks in advance

    template Silverlight Controls Databinding Templates datagrid Databinding Silverlight 2 silverlight 2.0

  • JKewley

    JKewley

    Member

    14 Points

    3 Posts

    Re: DataGrid - determine usercontrol I want in my DataGridTemplateColumn when binding

    Apr 23, 2008 07:00 PM | LINK

    I borrowed the approach mentioned here: http://silverlight.net/forums/p/11614/38274.aspx
    Which allows me to selectively add the user control of my choice at bind time:

    private void StackPanel_Loaded(object sender, System.Windows.RoutedEventArgs e) {
        StackPanel thePanel = sender as StackPanel;
       
    if(thePanel==null) return;

        LightingScene theScene = ((System.Windows.Controls.Grid)(thePanel.Parent)).DataContext as LightingScene;
       
    if(theScene==null) return;

        if (theScene.IsOn)
            thePanel.Children.Add(
    new GreenIndicatorControl());
       
    else {
            thePanel.Children.Add(
    new RedIndicatorControl());
        }
    }