Hi all,
I am a wpf newbie & loving it. However I have a layout problem which I hope someone can help me with. I need to build a persons attributes editor. These consist of 2 fixed attributes - first name & lastname, plus a variable bucket of other attributes such as age, sex etc.
I have build a dialog consists of grid which contains 2 textboxes for the fixed attributes and a listbox for the variable attributes.
<Grid Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">First Name:</Label>
<TextBox Name="tbFirstName" Grid.Column="2" Grid.Row="0" MinWidth="100" Margin="5" Text="{Binding Path=FirstName}"/>
<Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center">Last Name:</Label>
<TextBox Name="tbLastName" Grid.Column="2" Grid.Row="1" MinWidth="100" Margin="5" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}"/>
<ListBox Name="lstAttributes" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{Binding Path=Attributes, UpdateSourceTrigger=PropertyChanged}"/>
<StackPanel Orientation="Horizontal" Grid.Row="3" Grid.ColumnSpan="3" HorizontalAlignment="Right">
<Button Name="btnOk" IsDefault="True" Click="btnOk_Click" Grid.Column="0" Grid.Row="2" MinWidth="60" Margin="5">Ok</Button>
<Button Name="btnCancel" IsCancel="True" Grid.Column="0" Grid.Row="2" MinWidth="60" Margin="5">Cancel</Button>
</StackPanel>
</Grid>
I have a data layer that returns a person object which is bound. This contains a list of attributes that binds to the listbox. To support attributes of different types these derive from a common base class. i.e. IntegerAttribute : AttributeBase is used to represent the 'Age' attribute.
I then use data templates to render the correct controls depending on the type of attribute:
<Window.Resources>
<DataTemplate DataType="{x:Type reg:IntegerAttribute}">
<StackPanel Orientation="Horizontal">
<TextBlock Grid.Column="1" Text="{Binding Name}"/>
<TextBox Grid.Column="3" Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type reg:TextAttribute}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBox Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type reg:SingleChoiceAttribute}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<ComboBox ItemsSource="{Binding Path=Choices, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
But here is the problem. I want the variable set attributes to appear in the same columns as the fixed attributes. I tried using SharedSizeGroup but this does not seem to work.
Many thanks,
NickD