如何在Wpf Datagrid中为所选行显示添加的按钮

时间:2012-01-04 23:50:07

标签: wpf

我有一个带编辑,更新和取消按钮的Wpf Datagrid。最初编辑按钮可见,单击编辑按钮更新,取消按钮可见。

这里的问题是当我点击编辑按钮时,更新和取消按钮对于网格中的所有行都是可见的。我希望它只对网格中的选定行可见。

<DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                     <StackPanel Name="Stkbuttons">
                           <Button Name="BtnEdit" Content="Edit" Width="Auto" Click="BtnEdit_Click" Visibility="{Binding RelativeSource={RelativeSource                         FindAncestor, AncestorType={x:Type Window}}, Path=BtnEditVisibility}"></Button>
                          <Button Name="BtnUpdate" Content="Update" Width="Auto" Click="BtnUpdate_Click" Visibility="{Binding RelativeSource=                           {RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=BtnUpdateVisibility}"></Button>
                           <Button Name="BtnCancel" Content="Cancel" Width="Auto" Click="BtnCancel_Click" Visibility="{Binding RelativeSource=                          {RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=BtnCancelVisibility}"></Button>
                      </StackPanel>
                  </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>

public static readonly DependencyProperty BtnEditVisibilityProperty = DependencyProperty.Register("BtnEditVisibility", typeof(Visibility), typeof(OrderExecution));
     public Visibility BtnEditVisibility
     {
         get { return (Visibility)GetValue(BtnEditVisibilityProperty); }
         set { SetValue(BtnEditVisibilityProperty, value); }
     }

public static readonly DependencyProperty BtnUpdateVisibilityProperty = DependencyProperty.Register("BtnUpdateVisibility", typeof(Visibility), typeof(OrderExecution));
     public Visibility BtnUpdateVisibility
     {
         get { return (Visibility)GetValue(BtnUpdateVisibilityProperty); }
         set { SetValue(BtnUpdateVisibilityProperty, value); }
     }

public static readonly DependencyProperty BtnCancelVisibilityProperty = DependencyProperty.Register("BtnCancelVisibility", typeof(Visibility), typeof(OrderExecution));
     public Visibility BtnCancelVisibility
     {
         get { return (Visibility)GetValue(BtnCancelVisibilityProperty); }
         set { SetValue(BtnCancelVisibilityProperty, value); }
     }

private void Window_Loaded(object sender, RoutedEventArgs e)
     {
          // By default display editButton
         BtnUpdateVisibility = Visibility.Collapsed;
         BtnCancelVisibility = Visibility.Collapsed;
     }

private void BtnUpdate_Click(object sender, RoutedEventArgs e)
     {
         BtnEditVisibility = Visibility.Visible;
         BtnUpdateVisibility = Visibility.Collapsed;
         BtnCancelVisibility = Visibility.Collapsed;
     }

 private void BtnEdit_Click(object sender, RoutedEventArgs e)
     {
        BtnEditVisibility = Visibility.Collapsed;
         BtnUpdateVisibility = Visibility.Visible;
         BtnCancelVisibility = Visibility.Visible;
    }

1 个答案:

答案 0 :(得分:0)

在启用按钮之前,请转到单击的“编辑”按钮的父级,只获取他的孩子按钮(更新 - 取消),并仅更改这两个按钮的可见性。