Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4054448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:34:35+00:00 2026-05-20T14:34:35+00:00

I have the following code in XAML: <data:DataGridTemplateColumn> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image x:Name=picture Width=200 Height=130

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:34:36+00:00Added an answer on May 20, 2026 at 2:34 pm

    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; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given the following XAML code: <Canvas Name=MainView> <Canvas Name=TriangleElement Width=50 Height=50 Canvas.Left=110 Canvas.Top=100> <Canvas.RenderTransform>
I have the following XAML: <ListBox HorizontalAlignment=Left Margin=0,6,0,10 Name=listBox1 Width=468 ItemsSource={Binding} Grid.ColumnSpan=1> <ItemsControl.ItemTemplate> <DataTemplate>
I have the following XAML Code: <sdk:DataGrid Margin=58,8,52,18 Name=dataGridTickets> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn x:Name=ticketNoColumn Header=Ticket No.
I have the following XAML: <Grid x:Name=root> <Grid.RowDefinitions> <RowDefinition Height=*/> <RowDefinition Height=Auto/> </Grid.RowDefinitions> <Grid.Resources>
I have the following XAML: <Window.Resources> <myApp:ImageDisplayConvertor x:Key=ImageDisplayConvertor /> </Window.Resources> <DataGridTemplateColumn Header=Icon> <DataGridTemplateColumn.CellTemplate >
I have the following simple code that draws rectangle <Canvas Name=MainImageLayer > <Image >
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I have the following code: <Window x:Class=kkk.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <Window.Resources> <Style
I have following code class User attr_accessor :name end u = User.new u.name =
Let's pretend I have the following xaml... <UserControl.Resources> <local:ViewModel x:Name=viewModel /> <local:LoadChildrenValueConverter x:Name=valueConverter />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.