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>
-
\$\begingroup\$ Could you explain how is this user control meant to be used? \$\endgroup\$svick– svick2013年12月05日 17:34:42 +00:00Commented 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\$hiding.ape– hiding.ape2013年12月09日 13:20:20 +00:00Commented Dec 9, 2013 at 13:20
1 Answer 1
There are 2 things that I could critisize on your code with the information you gave us:
- Don't load style resources inside the usercontrol. Let the application hold control over how to style a usercontrol
- 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
-
\$\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– svick2013年12月05日 17:37:14 +00:00Commented 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\$RononDex– RononDex2013年12月06日 07:03:00 +00:00Commented Dec 6, 2013 at 7:03