【问题标题】:XAML => I can't access Datagrid parent from Group childXAML => 我无法从 Group child 访问 Datagrid parent
【发布时间】:2017-08-01 02:52:00
【问题描述】:

我喜欢从放置在 DataGrid/GroupStyle/ContainerStyle/Style[GroupItem]/Setter[template]/ControlTemplate/Expander/Header/DockPanel/StackPanel 中的 Behavior (pnlDgSubFooter) 绑定 datagrid 父级 但是DockPanel的Binding不能离开。 在 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();
    }

}

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    这是有效的:

    <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>
    

    只是设计师没有解决。

    问候。

    【讨论】:

      猜你喜欢
      • 2016-03-22
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多