无法在用户控件的DataGrid RowDetailsTemplate中填充DataGrid行

时间:2012-02-10 02:51:41

标签: c# wpf xaml datagrid

尝试在我的用户控件中填充 DataGrid 时遇到问题,该用户控件嵌入在父 DataGrid RowDetailsTemplate 中。 子DataGrid (位于用户控件内)应填充从其父DataGrid继承的相关信息。不知何故,它没有按预期工作。但是,当我尝试在我的子用户控件中绑定文本块时,它可以工作!希望有人可以帮助我: - )

非常感谢

这是我的父DataGrid:

 <Custom:DataGrid x:Name="MainDataGrid"
                     Grid.Row="1"
                     Margin="10,10,10,12"
                     HorizontalAlignment="Stretch"
                     AlternatingRowBackground="AliceBlue"
                     AlternationCount="2"
                     AutoGenerateColumns="False"
                     EnableColumnVirtualization="false"
                     EnableRowVirtualization="false"
                     IsReadOnly="False"
                     ItemsSource="{Binding ChangeOverData}"
                     RowDetailsVisibilityMode="Visible"
                     SelectedItem="{Binding Path=ChangeOverDetailsSelected,
                                            Mode=TwoWay,
                                            Source={StaticResource GridOverviewViewModel}}"
                     SelectionMode="Single"
                     SelectionUnit="FullRow">

        <Custom:DataGrid.Background>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0" Color="#F7BFEE86" />
                <GradientStop Offset="0.966" Color="#13A7E4FF" />
            </LinearGradientBrush>
        </Custom:DataGrid.Background>
        <Custom:DataGrid.Columns>
            <Custom:DataGridTemplateColumn Header="">

                <Custom:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Button Name="btnWIP"
                                    Width="20"
                                    Height="20"
                                    HorizontalAlignment="Left"
                                    Command="{Binding Source={StaticResource GridOverviewViewModel},
                                                      Path=ButtonReloadWIPDetails}"
                                    Content="WIP"
                                    Style="{DynamicResource RoundGlassButton}">

                            </Button>
                        </StackPanel>
                    </DataTemplate>

                </Custom:DataGridTemplateColumn.CellTemplate>

            </Custom:DataGridTemplateColumn>
            <Custom:DataGridTextColumn Binding="{Binding StartDate,
                                                         StringFormat={}{0:dd/MM/yyyy}}"
                                       Header="StartDate"
                                       IsReadOnly="True" />
            <Custom:DataGridTextColumn Binding="{Binding MacNo}"
                                       Header="MacNo"
                                       IsReadOnly="True" />
            <Custom:DataGridTextColumn Binding="{Binding PosNo}"
                                       Header="Pos"
                                       IsReadOnly="True" />

            <Custom:DataGridTextColumn Binding="{Binding Position}"
                                       Header="Position"
                                       IsReadOnly="True" />

            <Custom:DataGridTextColumn Binding="{Binding CurMO}"
                                       Header="CurrentMO"
                                       IsReadOnly="True" />

            <Custom:DataGridTextColumn Binding="{Binding NextMO}"
                                       Header="NextMO"
                                       IsReadOnly="True">

                <Custom:DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Foreground" Value="{Binding MOFontColor}" />
                    </Style>
                </Custom:DataGridTextColumn.ElementStyle>
            </Custom:DataGridTextColumn>


        </Custom:DataGrid.Columns>
        <Custom:DataGrid.RowDetailsTemplate>
            <DataTemplate>

                    <local:DataGridRowDetails x:Name="CODetails"
                    />

            </DataTemplate>
        </Custom:DataGrid.RowDetailsTemplate>

        <Custom:DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <Custom:DataGridRowsPresenter />
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </Custom:DataGrid.GroupStyle>
    </Custom:DataGrid>

这是我的子用户控件 - 其中包含另一个DataGrid

<Custom:DataGrid Name="dataGrid1"
                     Width="Auto"
                     Margin="0,0,0,45"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     HorizontalContentAlignment="Stretch"
                     VerticalContentAlignment="Stretch"
                     AlternatingRowBackground="Azure"
                     AlternationCount="2"
                     AutoGenerateColumns="False"
                     ItemsSource="{Binding}"
                     >

        <Custom:DataGrid.Columns>

            <Custom:DataGridTextColumn Binding="{Binding FIF}"
                                       Header="FIF"
                                       IsReadOnly="True" />

            <Custom:DataGridTextColumn Binding="{Binding MacNo}"
                                       Header="MacNo"
                                       IsReadOnly="True" />

            <Custom:DataGridTextColumn Binding="{Binding NomQty}"
                                       Header="NomQty"
                                       IsReadOnly="True" />

        </Custom:DataGrid.Columns>

    </Custom:DataGrid>
    <TextBlock Name="textBlock1"
               Width="85"
               Height="23"
               Margin="3,83,0,0"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Text="{Binding Path=NextMO}"                   
               />

1 个答案:

答案 0 :(得分:1)

这看起来并不正确:

ItemsSource="{Binding}"

或者您的物品实际上是收藏品吗?如果不是,您最好指定内部集合的路径。