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>

\$\endgroup\$
  • \$\begingroup\$ Could you explain how is this user control meant to be used? \$\endgroup\$ – svick Dec 5 '13 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\$ – hiding.ape Dec 9 '13 at 13:20
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
\$\endgroup\$
  • \$\begingroup\$ How exactly would another kind of panel help here? I think that Grid is ideal for this kind of label-value UIs. \$\endgroup\$ – svick Dec 5 '13 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\$ – RononDex Dec 6 '13 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.