Search code examples
c#silverlightdatagridcode-behinddatagridtemplatecolumn

Silverlight codebehind from XAML


I have the following code in XAML:

<data:DataGridTemplateColumn>

   <data:DataGridTemplateColumn.CellTemplate>

      <DataTemplate>

         <Image x:Name="picture" Width="200" Height="130" Visibility="Visible"/> 

      </DataTemplate>

   </data:DataGridTemplateColumn.CellTemplate>

</data:DataGridTemplateColumn>

How would do I this in the code behind (C#), without using this XAML code?

EDIT:

Here is the solution I am using:

Creating a Silverlight DataTemplate in code

This lets me do exactly what I want.


Solution

  • For example, you have a simple DataGrid

        <sdk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn CellTemplate="{StaticResource ImageCellTemplate}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    

    If you want to have several templates applied to a single DataGridRow, you can change visibility of internal parts inside the template:

        <DataTemplate x:Key="ImageCellTemplate">
            <Grid>
                <StackPanel Orientation="Horizontal" Visibility="{Binding IsImageTemplate}">
                    <Image Width="20" Height="20"/>
                    <TextBlock Text="{Binding Title}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Visibility="{Binding IsTextBoxTemplate}">
                    <TextBlock Text="{Binding Id}" Foreground="Red"/>
                    <TextBox Text="{Binding Title}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    

    There is the part with Image and the part with TextBox that are bound to the properties of an item view model (IsImageTemplate and IsTextBoxTemplate respectively). They are mutually exclusive and panels will not cover each other.

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            var items = new List<ItemViewModel>()
            {
                new ItemViewModel{Id = 1, Title="First", IsImage = true},
                new ItemViewModel{Id = 2, Title="Second", IsImage = false},
                new ItemViewModel{Id = 3, Title="Third", IsImage = false}
            };
            this.DataContext = new MainViewModel { Items = items };
        }
    }
    
    public class MainViewModel
    {
        public List<ItemViewModel> Items { get; set; }
    }
    
    public class ItemViewModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public bool IsImage { get; set; }
    
        public Visibility IsImageTemplate
        {
            get { return (IsImage == true) ? Visibility.Visible : Visibility.Collapsed; }
        }
    
        public Visibility IsTextBoxTemplate
        {
            get { return IsImage == false ? Visibility.Visible : Visibility.Collapsed; }
        }
    }