1
votes

I am trying to build a booking screen where the columns will be Monday through Sunday. There may be multiple rows but i don't think that is important. I want to add seven user controls and bind each one to a different day of the week.

I have a datagrid with a usercontrol in it and it is working. To get it working I had to bind the usercontrol to a specific property on the datarow (Mon). I want to be able to tell the usercontrol what property to bind to through xaml

The user control datagrid looks like this.

        <DataGrid 
            IsReadOnly="False"
            VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch"
            AutoGenerateColumns="False"
            CanUserAddRows="False"
            CanUserDeleteRows="False"
            SelectionMode="Extended"
            ItemsSource="{Binding Mon.BookingWeekBookingList, Mode=OneWay}"
            SelectionUnit="FullRow"
            EnableColumnVirtualization="True"
            EnableRowVirtualization="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Contract" Width="50*" IsReadOnly="True" Binding="{Binding Path=ContractId}" />
            <DataGridTextColumn Header="Tonnes" Width="50*" IsReadOnly="True" Binding="{Binding Path=Tonnes}" />
        </DataGrid.Columns>
    </DataGrid>

On the main window I add the userControll to the datagrid like this

                <DataGridTemplateColumn Header="Monday" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <local:BookingWeekIntervalView DataContext="{Binding}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

How Can I change it so I Specify the day of week in the main window binding? Rather than the itemsSource on the user control.

1
Just want to make sure I understand. You are trying to reuse the same custom UserControl for each day of the week, but have a different binding for each of them and you wish to set that binding from the MainWindow xaml instead of the UserControl xaml?Tronald

1 Answers

0
votes

I was actually quite close, the answer is to set the usercontrol's databinding like so

                ItemsSource="{Binding BookingWeekBookingList, Mode=OneWay}"

and then in the main window

                <DataGridTemplateColumn Header="Sunday" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <local:BookingWeekIntervalView DataContext="{Binding Path=Sun}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>