views:

36

answers:

2

Hi,

i have a question regarding some complex data-binding.
I want to be able to update a grid (which has the property "IsItemsHost" set to true)
dynamically whenever a data-binding occurs.
Actually i am using a CustomControl which is an ItemsControl and this
has the Grid in its ControlTemplate.

To be more specific, i bind the grid to some items and i want to change the number of grid rows depending on these items, add something like a header (one row containing some text), and set the items' Grid.Row and Grid.Column using some custom logic.

What is the easiest way to apply such behaviour whenever the bound data is updated?

Do i have to use a viewmodel that also contains the header data?

Thanks in advance.

Code of the CustomControl Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TimeTableControl">
<Style TargetType="{x:Type local:TimeTableControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TimeTableControl}">
                <Border Width="Auto" Height="Auto" BorderBrush="#FF4B5A9B" BorderThickness="4" CornerRadius="4" Margin="2" Padding="0" Background="White">
                    <Grid Width="Auto">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.1*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Viewbox>
                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DayCaption}"/>
                        </Viewbox>
                        <Border Grid.Row="1" BorderThickness="0,2,0,0" BorderBrush="#FF4B5A9B">
                            <Grid Name="ContentGrid" IsItemsHost="True">
                            </Grid>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>
A: 

Grid is used for layout. If you have a changing number of items in some collection, what you really want is ItemsControl, or more specific ListBox (if you want item selection, etc).

If you still want Grid-like behavior of individual rows, you may want to define a Grid in ItemsControl.ItemTemplate and play with Grid.IsSharedSizeScope at ItemsControl level. Alternatively, you may just use ListView instead to get the grid look and item selection in a package.

wpfwannabe
Actually i am using a CustomControl which is an ItemsControl and this has the Grid in its ControlTemplate. Sorry for not mentioning that.However, one of my problems is that i cannot add a header to this grid because it is databound and thus changing the rowdefinitions creates an exception...but i'll look into the IsSharedSizeScope-Property, thanks for pointing that out.
Ashwani Mehlem
Well, it is kind of hard to fill in the blanks and guess all the missing pieces. It is best if you could post some code/XAML to look at. Btw, `ItemsControl` has an `ItemsPanel` property so there is no need to mess with `ControlTemplate` if changing the underlying panel is all you want. `Grid` is special though, but the again depending on your exact requirements the optimal solution may be something completely different to your current one.
wpfwannabe
A: 

Update: I got it working by creating a custom panel that uses MeasureOverride and ArrangeOverride to update itself. This lets me adjust the panel to the children and I don't even need to use a grid. This also makes the control lookless.

Ashwani Mehlem