2013-05-26 53 views
2

C#WinForms中是否有任何方法僅在某些單元中添加按鈕,以便它們(按鈕)是單元的一部分?並添加這個按鈕的處理程序。 這需要以另一種形式插入特定單元格的值。但它不必完成表格中的所有單元格。 喜歡在圖片上。向datagridView中的某些單元添加按鈕

already added buttons http://i.snag.gy/vpD9Q.jpg

+0

它會是*給定列中的所有單元格嗎?如果是這樣,Google按鈕*按鈕*給你什麼? –

+1

如果這很容易......不,只在一些細胞中。 – streamdown

+0

無法回答。但+1,並會回來閱讀答案,如果可以做我想知道如何! –

回答

2

張貼這作爲一個答案,因爲OP要求它:

這是我的WPF採取的是:背後

<Window x:Class="MiscSamples.DataGridCustomCells" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:MiscSamples" 
     Title="DataGridCustomCells" Height="300" Width="300"> 
    <Window.Resources> 
     <BooleanToVisibilityConverter x:Key="BoolToVisibiltyConverter"/> 
     <Style TargetType="DataGridCell" x:Key="ButtonCell"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type DataGridCell}"> 
         <!-- ControlTemplate obtained with Expression Blend --> 
         <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
           Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
          <Grid> 
           <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
           <Button Height="16" Width="16" VerticalAlignment="Top" HorizontalAlignment="Right" 
             Visibility="{Binding (local:DataGridParameters.ShowButton), 
                  RelativeSource={RelativeSource TemplatedParent}, 
                  Converter={StaticResource BoolToVisibiltyConverter}}" 
             Command="{Binding CellButtonCommand, RelativeSource={RelativeSource AncestorType=Window}}" 
             CommandParameter="{Binding (local:DataGridParameters.ButtonValue), RelativeSource={RelativeSource TemplatedParent}}"/> 
          </Grid> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 


    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding LastName}"> 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell" BasedOn="{StaticResource ButtonCell}"> 
         <Setter Property="local:DataGridParameters.ShowButton" Value="{Binding DataContext.ShowButtonOnLastName, RelativeSource={RelativeSource Self}}"/> 
         <Setter Property="local:DataGridParameters.ButtonValue" Value="{Binding DataContext.LastName, RelativeSource={RelativeSource Self}}"/> 
        </Style> 
       </DataGridTextColumn.CellStyle>  
      </DataGridTextColumn> 

      <DataGridTextColumn Binding="{Binding FirstName}"> 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell" BasedOn="{StaticResource ButtonCell}"> 
         <Setter Property="local:DataGridParameters.ShowButton" Value="{Binding DataContext.ShowButtonOnFirstName, RelativeSource={RelativeSource Self}}"/> 
         <Setter Property="local:DataGridParameters.ButtonValue" Value="{Binding DataContext.FirstName, RelativeSource={RelativeSource Self}}"/> 
        </Style> 
       </DataGridTextColumn.CellStyle> 
      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Window> 

代碼:

public partial class DataGridCustomCells : Window 
    { 
     public ICommand CellButtonCommand { get; set; } 

     public DataGridCustomCells() 
     { 
      CellButtonCommand = new Command<object>(OnCellCommandExecuted){IsEnabled = true}; 

      InitializeComponent(); 

      DataContext = new List<DataGridSampleData> 
       { 
        new DataGridSampleData() {LastName = "Simpson", FirstName = "Homer", ShowButtonOnFirstName = true}, 
        new DataGridSampleData() {LastName = "Szyslak", FirstName = "Moe", ShowButtonOnLastName = true}, 
        new DataGridSampleData() {LastName = "Gumble", FirstName = "Barney", ShowButtonOnFirstName = true}, 
        new DataGridSampleData() {LastName = "Burns", FirstName = "Montgomery", ShowButtonOnLastName = true}, 
       }; 
     } 

     private void OnCellCommandExecuted(object parameter) 
     { 
      MessageBox.Show("Command Executed: " + parameter); 
     } 
    } 

樣品數據分類:

public class DataGridSampleData //TODO: Implement INotifyPropertyChanged 
{ 
    public string LastName { get; set; } 

    public string FirstName { get; set; } 

    public bool ShowButtonOnFirstName { get; set; } 

    public bool ShowButtonOnLastName { get; set; } 
} 

輔助類:

public static class DataGridParameters 
{ 
    public static readonly DependencyProperty ShowButtonProperty = DependencyProperty.RegisterAttached("ShowButton", typeof(bool), typeof(DataGridParameters)); 

    public static void SetShowButton(DependencyObject obj, bool value) 
    { 
     obj.SetValue(ShowButtonProperty, value); 
    } 

    public static bool GetShowButton(DependencyObject obj) 
    { 
     return (bool) obj.GetValue(ShowButtonProperty); 
    } 

    public static readonly DependencyProperty ButtonValueProperty = DependencyProperty.RegisterAttached("ButtonValue", typeof(object), typeof(DataGridParameters)); 

    public static void SetButtonValue(DependencyObject obj, object value) 
    { 
     obj.SetValue(ButtonValueProperty, value); 
    } 

    public static object GetButtonValue(DependencyObject obj) 
    { 
     return obj.GetValue(ButtonValueProperty); 
    } 
} 

通用基本DelegateCommand:

//Dead-simple implementation of ICommand 
    //Serves as an abstraction of Actions performed by the user via interaction with the UI (for instance, Button Click) 
    public class Command<T>: ICommand 
    { 
     public Action<T> Action { get; set; } 

     public void Execute(object parameter) 
     { 
      if (Action != null && parameter is T) 
       Action((T)parameter); 
     } 

     public bool CanExecute(object parameter) 
     { 
      return IsEnabled; 
     } 

     private bool _isEnabled; 
     public bool IsEnabled 
     { 
      get { return _isEnabled; } 
      set 
      { 
       _isEnabled = value; 
       if (CanExecuteChanged != null) 
        CanExecuteChanged(this, EventArgs.Empty); 
      } 
     } 

     public event EventHandler CanExecuteChanged; 

     public Command(Action<T> action) 
     { 
      Action = action; 
     } 
    } 

結果:

enter image description here

  • 請注意,我用Attached Properties以在現有的DataGridCell中啓用額外的行爲。
  • 此外,我使用DelegateCommand將這些按鈕全部重定向到相同的底層邏輯。
  • 我的例子中沒有一行代碼處理任何UI元素。全部通過DataBinding完成。這就是你在WPF中編碼的方式。
  • 這是一個非常基本和簡單的例子。您可以通過定義RowViewModel<TEntity>來創建一個更具可擴展性的解決方案,其中每個單元格值都包含在定義按鈕功能的CellViewModel<TValue>中,以及是否顯示按鈕等等。
  • 沒有「所有者抽象」,沒有P/Invoke,沒有可怕的代碼隱藏黑客。
  • WPF Rocks。只需在File -> New Project -> WPF Application中複製我的代碼並親自查看結果。
+0

謝謝你擴展答案。 – streamdown

相關問題