3
\$\begingroup\$

Below is a code snippet of how I have been laying out my WPF control XAML. I do it mainly because I get intellisense in the editor and I can change the implementation of the viewmodel and push the layout into a resource file.

However, I am not sure whether you gurus have a better insight into why I should not do this?

I'm just after a critique really.

<UserControl.Resources>
<ResourceDictionary>
 <ResourceDictionary.MergedDictionaries>
 <ResourceDictionary Source="../../Styles/BaseStyles.xaml"/>
 </ResourceDictionary.MergedDictionaries>
 <DataTemplate x:Key="LayoutRoot" 
 DataType="{x:Type ns:IViewModel}">
 <Grid Height="200" Width="300">
 <Grid.RowDefinitions>
 <RowDefinition Height="Auto" />
 <RowDefinition Height="Auto" />
 <RowDefinition Height="*"/>
 <RowDefinition Height="Auto" />
 </Grid.RowDefinitions>
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="Auto"/>
 <ColumnDefinition Width="*"/>
 </Grid.ColumnDefinitions>
 <Label Content="Some Label" Style="{StaticResource BoldLabel}" Margin="5, 0" 
 Target="{Binding ElementName=Lcb1}"/>
 <endorsements:LimitComboBox Grid.Row="0" Grid.Column="1" x:Name="Lcb1" 
 Limit="{Binding SomeData}" />
 <Label Grid.Row="1" Grid.Column="0" Content="Some Other Label" 
 Style="{StaticResource BoldLabel}" Margin="5, 0" Target={Binding ElementName=Lcb2/>
 <endorsements:LimitComboBox Grid.Row="0" Grid.Column="1" x:Name="Lcb2" 
 Limit="{Binding SomeOtherData}" />
 <StackPanel Grid.Row="3" Grid.Column="0" Orientation="Horizontal" HorizontalAlignment="Right" Grid.ColumnSpan="2">
 <Button Content="OK" Command="{Binding ApplyCommand}" IsDefault="True"/>
 <Button Content="Cancel" IsCancel="True"/>
 </StackPanel>
 </Grid>
 </DataTemplate>
</ResourceDictionary>
Mathieu Guindon
75.5k18 gold badges194 silver badges467 bronze badges
asked Dec 5, 2013 at 9:46
\$\endgroup\$
2
  • \$\begingroup\$ Could you explain how is this user control meant to be used? \$\endgroup\$ Commented Dec 5, 2013 at 17:34
  • \$\begingroup\$ It not so much about what the control does, but more about how to layout the markup. Instead of putting the markup directly into the body, I am placing it in a data template which may or may not be in the resources tag, then using a content presenter to display. \$\endgroup\$ Commented Dec 9, 2013 at 13:20

1 Answer 1

3
\$\begingroup\$

There are 2 things that I could critisize on your code with the information you gave us:

  1. Don't load style resources inside the usercontrol. Let the application hold control over how to style a usercontrol
  2. Try not to use a grid in this context, because it prevents the control from adopting itself to new sizes of the window. StackPanels and Dockpanel are much better for this
answered Dec 5, 2013 at 15:11
\$\endgroup\$
2
  • \$\begingroup\$ How exactly would another kind of panel help here? I think that Grid is ideal for this kind of label-value UIs. \$\endgroup\$ Commented Dec 5, 2013 at 17:37
  • \$\begingroup\$ Well it depends where you will integrate that user control. If you know that that part of the UI never will get resized then you are fine using grid yes. \$\endgroup\$ Commented Dec 6, 2013 at 7:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.