Can I create a UserControl which has different elements in it such as 'Rectangle' and 'TextBlock' and then call an instance of that UserControl in 'MainWindow.xaml' with a variable/value set to the 'Text' element for the TextBlock, and a 'Fill' for the Rectangles?
Therefore, I can just call an instance of the UserControl in the MainWindow, setting the values of each element, to save myself from replicating the UserControl XAML.
Say I have a MainWindow which is a Grid (3 x 3 say) of many Grids, all with the same structure. Instead of creating this 9 times:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Fill="{StaticResource colour1}" RadiusX="20" RadiusY="20" Margin="5"/>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="TITLE" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource textColour1}" FontSize="16" FontWeight="Medium"/>
<Rectangle Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Grid.RowSpan="2" Fill="{StaticResource colour1}" RadiusX="25" RadiusY="25" Margin="5,0, 5, 0"/>
<TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Text="TARGET" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource textColour1}" />
<Rectangle Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="1" Grid.RowSpan="2" Fill="{StaticResource colour1}" RadiusX="25" RadiusY="25" Margin="5,0, 5, 0"/>
<TextBlock Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="1" Text="ACTUAL" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource textColour1}" />
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="1" Text="0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource textColour1}" />
<TextBlock Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="1" Text="0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource textColour1}" />
</Grid>
Is there a way to create a UserControl called 'AreaTile', with a Binding back to the MainWindow XAML, and then call those elements in the MainWindow, such as:
<Grid Grid.Row="0" Grid.Column="0">
<local:AreaTile Title="FirstTitle" Colour="{StaticResource textColour1}"/>
</Grid>
<Grid Grid.Row="0" Grid.Column="1">
<local:AreaTile Title="SecondTitle" Colour="{StaticResource textColour2}"/>
</Grid>
<Grid Grid.Row="0" Grid.Column="2">
<local:AreaTile Title="ThirdTitle" Colour="{StaticResource textColour3}"/>
</Grid>
I have tried creating a TextBlock style and using a 'Setter' on 'Text' with a '{Binding Path=Tag}' but that didn't work.
I have also tried using C# with:
public static readonly DependencyProperty myAreaNameProperty =
DependencyProperty.Register("myAreaName", typeof(string), typeof(UserControl), new FrameworkPropertyMetadata(null));
But that seemed convoluted, and I thought there must be a way to do this purely with XAML.
Any help would be greatly appreciated.