I have 8 different XAML DataTemplates that are all very similar. Here are 2 of them:
<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">
<StackPanel>
<TextBlock Text="{Binding ClientVersion.Value}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}" />
<Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
<TextBlock Text="{Binding ServerVersion.Value}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
<StackPanel>
<TextBlock Text="{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
<Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
<TextBlock Text="{Binding ServerVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
</StackPanel>
</DataTemplate>
As you can see, the only difference is that they use a different Converter for the Binding of the Text property of the TextBlock. Is there any way for me to factor out the commonalities of these two DataTemplates? I have 6 more and updating them is getting very tedious, because everything is identical except for the Converter for the Binding of the Text property.
Is there a way to somehow factor this out into one template which can be parameterized somehow? Something like this would be cool (pseudo-code):
<DataTemplate x:Key="BaseCellContentTemplate">
<StackPanel>
<TextBlock Text="{??}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}" />
<Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
<TextBlock Text="{Binding ServerVersion.Value}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ConflictFieldStringCellContentTemplate" BaseTemplate="BaseCellContentTemplate">
<??>{Binding ClientVersion.Value}</??>
</DataTemplate>
<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate" BaseTemplate="BaseCellContentTemplate">
<??>{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}</??>
</DataTemplate>
If there is only one value, and you want to do it purely with templates, you might do:
<DataTemplate x:Key="VersionDisplayTemplate">
<StackPanel>
<TextBlock Text="{TemplateBinding Tag}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}" />
<Label Background="LightGray" Height="1" Margin="0, 4, -4, 2"></Label>
<TextBlock Text="{TemplateBinding Content}"
Foreground="{Binding Path=Mismatch, Converter={StaticResource mismatchBoolToBrushConverter}}"/>
</StackPanel>
</DataTemplate>
Now you can use it as:
<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">
<ContentPresenter
Tag="ABC"
Content="{Binding ClientVersion.Value}"
ContentTemplate="{StaticResource VersionDisplayTemplate}"
/>
</DataTemplate>
<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
<ContentPresenter
Tag="XYZ"
Content="{Binding ClientVersion.Value, Converter={StaticResource stringArrayToCommaDelimitedStringConverter}}"
ContentTemplate="{StaticResource VersionDisplayTemplate}"
/>
</DataTemplate>
One path you can try is to create a new User Control.
This User Control should contain the StackPanel, and this StackPanel should contain the TextBox, Label and TextBox.
You could implement the TextConverters as dependency properties.
The final set of DataTemplates would look like this:
<DataTemplate x:Key="ConflictFieldStringCellContentTemplate">
<local:VersionDisplayControl
ClientVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}" />
</DataTemplate>
<DataTemplate x:Key="ConflictFieldStringArrayCellContentTemplate">
<local:VersionDisplayControl
ClientVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}"
ServerVersionTextConverter="{StaticResource stringArrayToCommaDelimitedStringConverter}" />
</DataTemplate>
This is assuming that the User Control is able to access the source version information from some globally available source.
If not, the VersionDisplayControl will have to expose another public property, probably called VersionSource.
Related
I have a TreeView and want to get the selected item from it.
The Treeview itself is populated not manually, but from data in the code. Since it is populated this way, I'm not sure how to get information out of it.
Here is the XAML:
<TreeView Name="trvFamilies" HorizontalAlignment="Left" Margin="10,10,3,3" Width="340">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Scene}" ItemsSource="{Binding Characters}">
<StackPanel Orientation="Horizontal">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="New" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="Remove" Click="MenuItem_Click_1"></MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
<Image Source="{StaticResource ImageSceneRegular}" Margin="0,0,5,0" Width="64" Height="64"/>
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Characters.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:Character}">
<Border BorderThickness="1" Background="AliceBlue" CornerRadius="8,8,3,3">
<StackPanel Orientation="Horizontal" Margin="4" Background="White">
<Image Source="{Binding Img}" Margin="0,0,5,0" Width="64" Height="64" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" (" Foreground="Green" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
<TextBlock Text=" years)" Foreground="Green" />
</StackPanel>
</Border>
</DataTemplate>
</TreeView.Resources>
</TreeView>
It is populated by data from an ObservableCollection which is populated within the code and then assigned to the ItemSource of the treeview (and it works fine).
So let's say I had a TextBlock which sat outside the TreeView and wanted to populate it with information from the selected Character (that's the type which populates the drop down part of the TreeView), I don't know how to do that, be it using XAML only or code behind.
Any help on how to do this would be very much appreciated.
I have created a WPF app.In that I have a Datatemplate as follows
<DataTemplate x:Key="ItemTemplate">
<StackPanel>
<TextBlock Text="item"/>
<TextBlock Text="{Binding Number}"/>
</StackPanel>
</DataTemplate>
I have an ItemsControl like this
<ItemsControl ItemsSource="{Binding Items}"
Grid.Column="1"
Grid.Row="3"
ItemTemplate="{StaticResource ItemTemplateWithButton}" />
where I need a itemtemplate like this
<DataTemplate x:Key="ItemTemplateWithButton">
<StackPanel>
<StackPanel>
<TextBlock Text="item"/>
<TextBlock Text="{Binding Number}"/>
</StackPanel>
<StackPanel>
<Button>
<StackPanel>
<TextBlock Text="item"/>
<TextBlock Text="{Binding Number}"/>
</StackPanel>
</Button>
</StackPanel>
</StackPanel>
</DataTemplate>
Is there any possibility of reusing the datatemplate in the new itemscontrol?
You can use ContentControl too
<DataTemplate x:Key="ItemTemplate">
<StackPanel>
<TextBlock Text="item"/>
<TextBlock Text="{Binding Number}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ItemTemplateWithButton">
<StackPanel>
<ContentControl ContentTemplate="{StaticResource ItemTemplate}" />
<Button>
<ContentControl ContentTemplate="{StaticResource ItemTemplate}" />
</Button>
</StackPanel>
</DataTemplate>
What I understand by reading this answer and what Liero mentioned in the comments is it's possible to reuse a DataTemplate by using either ContentPresenter or ContentControl. However:
ContentPresenter is more lightweight.
ContentPresenter is designed to be used inside control templates.
ContnetPresenter is designed to be used as-is while ContentControl is designed to be extended (inherited from).
As a result, here is a solution based on what you asked:
<DataTemplate x:Key="ItemTemplate">
<StackPanel>
<TextBlock Text="item"/>
<TextBlock Text="{Binding Number}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ItemTemplateWithButton">
<StackPanel>
<ContentPresenter ContentTemplate="{StaticResource ItemTemplate}"/>
<StackPanel>
<Button Content="{Binding}" ContentTemplate="{StaticResource ItemTemplate}" />
</StackPanel>
</StackPanel>
</DataTemplate>
You could create a UserControl to hold the xaml you want to reuse:
<UserControl x:Class="StackOverflow.SharedControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel>
<TextBlock Text="item">
</TextBlock>
<TextBlock Text="{Binding Number}"></TextBlock>
</StackPanel>
</Grid>
</UserControl>
Then use this UserControl in both templates.
<DataTemplate x:Key="ItemTemplate">
<controls:SharedControl/>
</DataTemplate>
<DataTemplate x:Key="ItemTemplateWithButton">
<StackPanel>
<controls:SharedControl/>
<StackPanel>
<Button>
<StackPanel>
<TextBlock Text="item">
</TextBlock>
<TextBlock Text="{Binding Number}"></TextBlock>
</StackPanel>
</Button>
</StackPanel>
</StackPanel>
</DataTemplate>
I have a Combobox whose ItemsSource is an ObservableCollection of int values.
My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.
How can I set this 2 bindings? The following code does not compile:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
You need to remove the , so:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
I would like to see in the group header of a datagrid some additional information per column. The header consists of a Stackpanel with some sub Stackpanels in it.
Due to the fact that the user can adjust the size of the columns of the datagrid I have to adjust the size of the single header parts per binding the width of them to the width of the corresponding column:
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<StackPanel
Orientation="Horizontal"
Width="{Binding Source={x:Reference TextCol01}, Path=ActualWidth}" >
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Path=ItemCount}" FontSize="13" FontWeight="Bold"/>
<TextBlock Text=")"/>
</StackPanel>
<StackPanel
Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" ("/>
<TextBlock Text="{Binding Path=ItemCount}" FontSize="13" FontWeight="Bold"/>
<TextBlock Text=")"/>
</StackPanel>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
Without binding the Width (Width="{Binding Source={x:Reference TextCol01}, Path=ActualWidth}") the binding to the Name and the ItemCount of the CollectionViewGroup works perfect. But with binding the Width it fails.
I suppose it is to do with Binding Source. That changes the context or so. But I don't know what exactly is wrong with it.
Could anyone help? Thank you!
I figured it out. This way to bind the width is working well:
Width="{Binding ElementName=TextCol01, Path=ActualWidth}"
this doesn't work:
Width="{Binding Source={x:Reference TextCol01}, Path=ActualWidth}"
It works standalone but together with the bindings of ItemCount and Name it leads to a failure.
I have a UI that displays a pattern of "first name/last name". So I thought I would reuse the same template. But I am facing some issues getting the binding right.
Note:-
PrimaryContactDataContext is nothing but a class, with a property named "value" which implements the *INotifyPropertyChanged" interface.
<StackPanel>
<ContentControl DataContext="{Binding Path=PrimaryContactDataContext.Value,Mode=TwoWay}" ContentTemplate="{StaticResource PersonalDetailsTemplate}" />
</StackPanel>
// See the Reusable template below
<UserControl.Resources>
<DataTemplate x:Key="PersonalDetailsTemplate" >
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="30" Text="Name"></TextBlock>
<TextBox Width="110" Text="{Binding LastName}" IsReadOnly="True"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Width="30" Text="Title"></TextBlock>
<TextBox Width="110" Text="{Binding firstName}" IsReadOnly="True"></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
Set the Content of the ContentControl, not its DataContext:
<ContentControl Content="{Binding Path=PrimaryContactDataContext.Value,Mode=TwoWay}" ContentTemplate="{StaticResource PersonalDetailsTemplate}" />