XAML =>我无法从Group child访问Datagrid父级

时间:2017-03-10 17:00:37

标签: wpf xaml binding

我喜欢从位于DataGrid / GroupStyle / ContainerStyle / Style [GroupItem] / Setter [template] / ControlTemplate / Expander / Header / DockPanel / StackPanel中的Behavior(pnlDgSubFooter)绑定datagrid父级 但Binding不能离开DockPanel。 在ElementName中,只有" btnExpandAll"和他自己。 在Behavior中,LeDataGrid属性总是返回null(除非我绑定。或btnExpandAll)

<DataGrid Grid.Row="0" x:Name="dgMain" AutoGenerateColumns="False" SelectionUnit="FullRow" LoadingRow="dgMain_LoadingRow" MouseDown="dgMain_MouseDown" Sorting="dgMain_Sorting"
    CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserResizeRows="False" CanUserSortColumns="True" CanUserAddRows="False"
    Style="{StaticResource dg}" RowStyle="{StaticResource dgRow}" CellStyle="{StaticResource dgCell}" ColumnHeaderStyle="{StaticResource dgColHeader}" RowHeaderStyle="{StaticResource dgRowHeader}"
    ItemsSource="{Binding NotifyOnSourceUpdated=True, Source={StaticResource cvsElmts}}" HorizontalAlignment="Left" >
<!--DataGrid.DataContext><Binding Source="{StaticResource tblUsers}"/></DataGrid.DataContext-->
<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <mvvm:EventToCommand Command="{Binding SendCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=dgMain}" PassEventArgsToCommand="False"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.GroupStyle>
    <GroupStyle>
    <!-- Style for groups under the top level. -->
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Margin" Value="0,0,0,5"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander IsExpanded="{Binding DataContext.ExpandedAll, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" BorderThickness="1,1,1,5">
                                <Expander.Style>
                                    <Style TargetType="{x:Type Expander}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsBottomLevel}" Value="True">
                                                <Setter Property="Margin" Value="8,0,0,0" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Expander.Style>
                                <Expander.Header>
                                    <DockPanel>
                                        <Button Name="btnExpandAll" Command="{Binding DataContext.ExpandedAllCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Click="btnExpandAll_Click" ToolTip="{x:Static resx:resMain.lblExpandAll}" BorderThickness="0">
                                            <TextBlock Grid.Column="0" Text="&#xE155;" FontFamily="{StaticResource FntSymbol}" Foreground="{StaticResource scbBlack}" FontSize="12" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                        </Button>
                                        <TextBlock FontWeight="Bold" Text="" Margin="5,0,0,0"><Run Text="{Binding Path=Name, Mode=OneWay}" /><Run Text=" ("/><Run Text="{Binding Path=ItemCount, Mode=OneWay}" /><Run Text=" éléments)."/></TextBlock>
                                        <StackPanel Name="pnlDgSubFooter" HorizontalAlignment="Left" Orientation="Horizontal" >
                                            <i:Interaction.Behaviors>
                                                <Classes:BehaviorWithCommand LeDataGrid="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" LeGroupe="{Binding .}" />
                                            </i:Interaction.Behaviors>
                                        </StackPanel>
                                    </DockPanel>
                                </Expander.Header>
                                <Expander.Content>
                                    <Border BorderThickness="1" BorderBrush="{StaticResource scbGrey3}">
                                    <ItemsPresenter />
                                    </Border>
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
....

行为:

public class BehaviorWithCommand : Behavior<FrameworkElement> { // StackPanel
    public static readonly DependencyProperty LeDataGridProperty = DependencyProperty.Register(nameof(LeDataGrid), typeof(object), typeof(BehaviorWithCommand), new PropertyMetadata(null));

    public static readonly DependencyProperty LeGroupeProperty = DependencyProperty.Register(nameof(LeGroupe), typeof(object), typeof(BehaviorWithCommand), new PropertyMetadata(null));

    public object LeDataGrid {
        get { return (object)GetValue(LeDataGridProperty); }
        set { SetValue(LeDataGridProperty, value); }
    }

    public object LeGroupe {
        get { return (object)GetValue(LeGroupeProperty); }
        set { SetValue(LeGroupeProperty, value); }
    }

    protected override void OnAttached() {
        base.OnAttached();
        ((Panel)AssociatedObject).Children.Clear();
        var dg = new DataGrid();
        dg.Columns.Add(new DataGridTextColumn());
        dg.Columns.Add(new DataGridTextColumn());
        dg.Columns.Add(new DataGridTextColumn());
        //var dg = (DataGrid)LeDataGrid;
        foreach (var item in dg.Columns) {
            var g = new Grid() { MinWidth = 10 };
            g.SetBinding(Grid.WidthProperty, new System.Windows.Data.Binding("ActualWidth") { Source = item }); // dhHeadName DataGridColumn
            var t = new TextBox() { Margin = new Thickness(1, 0, 1, 0), Background = new SolidColorBrush(Color.FromRgb(200, 200, 200)), FontWeight = FontWeights.Bold, FontSize = 9, IsReadOnly = true };
            t.Text = "Coucou";
            g.Children.Add(t);
            ((Panel)AssociatedObject).Children.Add(g);
        }
    }

    protected override void OnDetaching() {
        base.OnDetaching();
    }

}

1 个答案:

答案 0 :(得分:0)

这是有效的:

<StackPanel Grid.ColumnSpan="2" Grid.Row="1" Name="pnlDgSubFooter" HorizontalAlignment="Left" Orientation="Horizontal" Margin="-20, 0, 0, 0">
    <i:Interaction.Behaviors>
        <Classes:BehaviorWithCommand LeDataGrid="{Binding ., ElementName=dgMain}" LeGroupe="{Binding .}" />
    </i:Interaction.Behaviors>
</StackPanel>

只是设计师没有解决它。

问候。