How to modify DataTemplate programmatically?
-
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
-
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
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.
-
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.
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?
-
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
If this is WPF, have you looked at DataTemplateSelector[^]?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
If this is WPF, have you looked at DataTemplateSelector[^]?
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thank you. But what if I don't want to use DataTemplateSelector?
-
Thank you. But what if I don't want to use DataTemplateSelector?
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:
-
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?
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.
-
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
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.
-
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.
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.
-
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.
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"