Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to modify DataTemplate programmatically?

How to modify DataTemplate programmatically?

Scheduled Pinned Locked Moved WPF
wpfcsswcftutorialquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tesic Goran
    wrote on last edited by
    #1

    Hi, I'm just wondering how to modify DataTemplate programmatically? Here's my XAML code:

                    <ListView Grid.Row="2" Name="lstvTCKeyValue" Margin="10,33,10,0" ItemsSource="{Binding}" MouseDoubleClick="lstvTCKeyValue\_MouseDoubleClick" KeyDown="lstvTCKeyValue\_KeyDown" SelectionChanged="lstvTCKeyValue\_SelectionChanged" GridViewColumnHeader.Click="lstvTCKeyValue\_Click">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.Resources>
                            <DataTemplate x:Key="Column1">
                                <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                    <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=test\_config\_key}">
                                        <TextBlock.ToolTip>
                                            <TextBlock Text="{Binding Path=test\_config\_key}"></TextBlock>
                                        </TextBlock.ToolTip>
                                    </TextBlock>
                                </Border>
                            </DataTemplate>
    

    I'd like to modify border settings of data template named "Column1". Thank you in advance. Goran

    A M S 3 Replies Last reply
    0
    • T Tesic Goran

      Hi, I'm just wondering how to modify DataTemplate programmatically? Here's my XAML code:

                      <ListView Grid.Row="2" Name="lstvTCKeyValue" Margin="10,33,10,0" ItemsSource="{Binding}" MouseDoubleClick="lstvTCKeyValue\_MouseDoubleClick" KeyDown="lstvTCKeyValue\_KeyDown" SelectionChanged="lstvTCKeyValue\_SelectionChanged" GridViewColumnHeader.Click="lstvTCKeyValue\_Click">
                          <ListView.ItemContainerStyle>
                              <Style TargetType="ListViewItem">
                                  <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                  <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                              </Style>
                          </ListView.ItemContainerStyle>
                          <ListView.Resources>
                              <DataTemplate x:Key="Column1">
                                  <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                      <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=test\_config\_key}">
                                          <TextBlock.ToolTip>
                                              <TextBlock Text="{Binding Path=test\_config\_key}"></TextBlock>
                                          </TextBlock.ToolTip>
                                      </TextBlock>
                                  </Border>
                              </DataTemplate>
      

      I'd like to modify border settings of data template named "Column1". Thank you in advance. Goran

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You cannot modify DataTemplates in Silverlight. The best you can do is keep alternate templates ready and assign them as required.

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

      T 1 Reply Last reply
      0
      • A Abhinav S

        You cannot modify DataTemplates in Silverlight. The best you can do is keep alternate templates ready and assign them as required.

        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

        T Offline
        T Offline
        Tesic Goran
        wrote on last edited by
        #3

        Thank you for idea. I added new alternative data template to XAML file in this way:

                                <DataTemplate x:Key="Column3">
                                    <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                        <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                                    </Border>
                                </DataTemplate>
                                <DataTemplate x:Key="Column3\_Alternative">
                                    <Border BorderThickness="0,0,0,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                        <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                                    </Border>
                                </DataTemplate>
        

        And I change data template when I click button on the form. I've used this the code:

              DataTemplate dt = lstvTCKeyValue.FindResource("Column3\_Alternative") as DataTemplate;
              lstvTCKeyValue.ItemTemplate = dt;
        

        But, it doesn't work. Any suggestion?

        A 1 Reply Last reply
        0
        • T Tesic Goran

          Hi, I'm just wondering how to modify DataTemplate programmatically? Here's my XAML code:

                          <ListView Grid.Row="2" Name="lstvTCKeyValue" Margin="10,33,10,0" ItemsSource="{Binding}" MouseDoubleClick="lstvTCKeyValue\_MouseDoubleClick" KeyDown="lstvTCKeyValue\_KeyDown" SelectionChanged="lstvTCKeyValue\_SelectionChanged" GridViewColumnHeader.Click="lstvTCKeyValue\_Click">
                              <ListView.ItemContainerStyle>
                                  <Style TargetType="ListViewItem">
                                      <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                      <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                                  </Style>
                              </ListView.ItemContainerStyle>
                              <ListView.Resources>
                                  <DataTemplate x:Key="Column1">
                                      <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                          <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=test\_config\_key}">
                                              <TextBlock.ToolTip>
                                                  <TextBlock Text="{Binding Path=test\_config\_key}"></TextBlock>
                                              </TextBlock.ToolTip>
                                          </TextBlock>
                                      </Border>
                                  </DataTemplate>
          

          I'd like to modify border settings of data template named "Column1". Thank you in advance. Goran

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          If this is WPF, have you looked at DataTemplateSelector[^]?

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          T 1 Reply Last reply
          0
          • M Mark Salsbery

            If this is WPF, have you looked at DataTemplateSelector[^]?

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            T Offline
            T Offline
            Tesic Goran
            wrote on last edited by
            #5

            Thank you. But what if I don't want to use DataTemplateSelector?

            M 1 Reply Last reply
            0
            • T Tesic Goran

              Thank you. But what if I don't want to use DataTemplateSelector?

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Is that a joke? You can use whatever you want. I only tried to provide another possible solution.

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • T Tesic Goran

                Thank you for idea. I added new alternative data template to XAML file in this way:

                                        <DataTemplate x:Key="Column3">
                                            <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                                <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                                            </Border>
                                        </DataTemplate>
                                        <DataTemplate x:Key="Column3\_Alternative">
                                            <Border BorderThickness="0,0,0,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                                <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                                            </Border>
                                        </DataTemplate>
                

                And I change data template when I click button on the form. I've used this the code:

                      DataTemplate dt = lstvTCKeyValue.FindResource("Column3\_Alternative") as DataTemplate;
                      lstvTCKeyValue.ItemTemplate = dt;
                

                But, it doesn't work. Any suggestion?

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #7

                There is barely any difference between the two templates (except for a margin). Try changing the color or something.

                The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

                1 Reply Last reply
                0
                • T Tesic Goran

                  Hi, I'm just wondering how to modify DataTemplate programmatically? Here's my XAML code:

                                  <ListView Grid.Row="2" Name="lstvTCKeyValue" Margin="10,33,10,0" ItemsSource="{Binding}" MouseDoubleClick="lstvTCKeyValue\_MouseDoubleClick" KeyDown="lstvTCKeyValue\_KeyDown" SelectionChanged="lstvTCKeyValue\_SelectionChanged" GridViewColumnHeader.Click="lstvTCKeyValue\_Click">
                                      <ListView.ItemContainerStyle>
                                          <Style TargetType="ListViewItem">
                                              <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                              <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                                          </Style>
                                      </ListView.ItemContainerStyle>
                                      <ListView.Resources>
                                          <DataTemplate x:Key="Column1">
                                              <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                                  <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=test\_config\_key}">
                                                      <TextBlock.ToolTip>
                                                          <TextBlock Text="{Binding Path=test\_config\_key}"></TextBlock>
                                                      </TextBlock.ToolTip>
                                                  </TextBlock>
                                              </Border>
                                          </DataTemplate>
                  

                  I'd like to modify border settings of data template named "Column1". Thank you in advance. Goran

                  S Offline
                  S Offline
                  SledgeHammer01
                  wrote on last edited by
                  #8

                  It seems like everybody has neglected to give you the *RIGHT* answer and just continued to support your wrong direction. You should only have a single template and use triggers, VisualStateManager, DependencyProperties, etc. to modify your data template. The only time having multiple templates is appropriate is if you are making a control that needs to look different in different situations like one look for Aero and one look for classic, etc. Doesn't seem like your situation applies in this case, so I'd stick to the single template.

                  T P 2 Replies Last reply
                  0
                  • S SledgeHammer01

                    It seems like everybody has neglected to give you the *RIGHT* answer and just continued to support your wrong direction. You should only have a single template and use triggers, VisualStateManager, DependencyProperties, etc. to modify your data template. The only time having multiple templates is appropriate is if you are making a control that needs to look different in different situations like one look for Aero and one look for classic, etc. Doesn't seem like your situation applies in this case, so I'd stick to the single template.

                    T Offline
                    T Offline
                    Tesic Goran
                    wrote on last edited by
                    #9

                    Actually, I've tried the code with setting list view's ItemTemplate instead of setting columns's CellTemplate what I needed. Therefore, I applied the the method with 2 data templates in list view's resource:

                        <DataTemplate x:Key="Column3\_ShowStandColumn">
                            <Border BorderThickness="0,0,1,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                            </Border>
                        </DataTemplate>
                        <DataTemplate x:Key="Column3\_HideStandColumn">
                            <Border BorderThickness="0,0,0,0" BorderBrush="Gray" Margin="-6,0,-6,0">
                                <TextBlock Margin="2,1,1,1" Text="{Binding Path=stand\_name}"></TextBlock>
                            </Border>
                        </DataTemplate>
                    

                    And this code in combo box's SelectionChanged event:

                        if (selectedStand.stand\_name != "Все")
                        {
                          ...
                          DataTemplate dt = lstvTCKeyValue.FindResource("Column3\_HideStandColumn") as DataTemplate;
                          gv.Columns\[2\].CellTemplate = dt;
                          ...
                        }
                        else
                        {
                          ...
                          DataTemplate dt = lstvTCKeyValue.FindResource("Column3\_ShowStandColumn") as DataTemplate;
                          gv.Columns\[2\].CellTemplate = dt;
                          ...
                        }
                    

                    Thank you all for suggestions and help.

                    1 Reply Last reply
                    0
                    • S SledgeHammer01

                      It seems like everybody has neglected to give you the *RIGHT* answer and just continued to support your wrong direction. You should only have a single template and use triggers, VisualStateManager, DependencyProperties, etc. to modify your data template. The only time having multiple templates is appropriate is if you are making a control that needs to look different in different situations like one look for Aero and one look for classic, etc. Doesn't seem like your situation applies in this case, so I'd stick to the single template.

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Indeed, and for those who are interested, here's an example of a template that can be applied to a checkbox. Basically, this DataTemplate uses a trigger to change a control template when the IsSelected value changes. Just for clarity - this isn't the best way to restyle a checkbox, it just demonstrates using a DataTemplate and ControlTemplate elements. BTW - this is a purely XAML based alternative to a DataTemplateSelector.

                      <ControlTemplate x:Key="OnTemplate">
                      <Grid ToolTip="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}">
                      <Rectangle HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Width="80" Height="20" RadiusX="3" RadiusY="3">
                      <Rectangle.Fill>
                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                      <GradientStop Color="#FF6183DC" Offset="0"/>
                      <GradientStop Color="#FFC6CDE0" Offset="1"/>
                      </LinearGradientBrush>
                      </Rectangle.Fill>
                      </Rectangle>
                      <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="ON" TextWrapping="Wrap" Margin="8,0,0,0" FontSize="16" FontWeight="Bold" Foreground="#FFFEFCFC"/>
                      <Rectangle RadiusX="3" RadiusY="3" HorizontalAlignment="Left" Margin="40,0,0,0" VerticalAlignment="Center" Width="40" Height="20">
                      <Rectangle.Fill>
                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                      <GradientStop Color="#FFB5B1B1" Offset="0.004"/>
                      <GradientStop Color="White" Offset="1"/>
                      </LinearGradientBrush>
                      </Rectangle.Fill>
                      </Rectangle>
                      </Grid>
                      </ControlTemplate>
                      <ControlTemplate x:Key="OffTemplate">
                      <Grid ToolTip="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}">
                      <Rectangle HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Width="80" Height="20" RadiusX="3" RadiusY="3">
                      <Rectangle.Fill>
                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                      <GradientStop Color="#FFE0E7F8" Offset="0"/>
                      <GradientStop Color="#FFF2F4FA" Offset="1"/>
                      </LinearGradientBrush>
                      </Rectangle.Fill>
                      </Rectangle>
                      <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="OFF" TextWrapping="Wrap" Margin="44,0,0,0" FontSize="16" FontWeight="Bold" Foreground="#FFB3ABAB"/>
                      <Rectangle RadiusX="3"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups