2017-03-10 4 views
1

DataGrid/GroupStyle/ContainerStyle/Styleに配置された動作(pnlDgSubFooter)からデータグリッドの親をバインドしたい[GroupItem]/Setter [テンプレート]/ControlTemplate/Expander /ヘッダー/ DockPanel/StackPanel ただし、バインディングはDockPanelから離れることはできません。 ElementNameの には、 "btnExpandAll"と自分だけがあります。行動の 、LeDataGridプロパティの戻りなかれヌルXAML =>グループの子からDatagridの親にアクセスできない

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

行動(私は結合するか、またはbtnExpandAll場合を除く。):これは、働いている

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(); 
    } 

} 

答えて

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> 

はそれだけですそのデザイナーはそれを解決しません。

よろしくお願いいたします。

関連する問題