タグ: DataGrid

  • How to add button to WPF DataGrid column in code programmatically

    private void DataGridScanChannel_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
            {            
                var styleCell = new Style(typeof(DataGridCell))
                {
                    Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right) }
                };
                e.Column.Header = MemoryChannelHeader[e.PropertyName];
                e.Column.CellStyle = styleCell;
                DataGridTextColumn txtcol = e.Column as DataGridTextColumn;
    
                switch (e.PropertyName)
                {
                    case "Prog_No":
                        var buttonTemplate = new FrameworkElementFactory(typeof(System.Windows.Controls.Button));
                        var text = new FrameworkElementFactory(typeof(TextBlock));
                        text.SetValue(TextBlock.TextProperty, "Start");
                        buttonTemplate.AppendChild(text);
                        //buttonTemplate.AddHandler(System.Windows.Controls.Primitives.ButtonBase.ClickEvent, new RoutedEventHandler((o, e) => System.Windows.MessageBox.Show("hi")));
                        buttonTemplate.AddHandler(System.Windows.Controls.Button.ClickEvent, new RoutedEventHandler(Btn_Click));
                        DataGridScanChannel.Columns.Add(new DataGridTemplateColumn()
                        {
                            Header = "Scan",
                            DisplayIndex = 0,
                            CellTemplate = new DataTemplate() { VisualTree = buttonTemplate }
                        });
                        break;
                    case "Prog_Mode":
                        e.Cancel = true;
                        break;
                    case "Prog_Freq_Lower":
                        txtcol.Binding = new System.Windows.Data.Binding(e.PropertyName) { StringFormat = "{0:N0}" };
                        txtcol.Width = 100;
                        //e.Column.DisplayIndex = 1;
                        break;
                    case "Prog_Freq_Upper":
                        txtcol.Binding = new System.Windows.Data.Binding(e.PropertyName) { StringFormat = "{0:N0}" };
                        txtcol.Width = 100;
                        //e.Column.DisplayIndex = 2;
                        break;
                    default:
                        break;
                }
            }
  • WPF DataGrid Control Tips

    DataGridのRowのBackgroundカラーを変更する

    <Window.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Setters>
            <Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
        </Style.Setters>            
    </Style>
    </Window.Resources>
    
    
    
    <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
    <DataGrid.Columns>                            
        <DataGridTextColumn Header="Title" Binding="{Binding Path=Title}"/>
    </DataGrid.Columns>
    </DataGrid>
    public class ItemData
    {
        public string Title { get; set; }
        public string StatusColor { get; set; }
    }
    
    DataGrid DateGrid1;
    List<ItemData> list1 = new();
    
    ItemData item1 = new();
    item1.Title = "Title001"; 
    item1.StatusColor = "Blue";
    list1.Add(item1);
    ItemData item2 = new();
    item2.Title = "Title002"; 
    item2.StatusColor = "LightBlue";
    list1.Add(item2);
    
    DateGrid1.ItemsSource = list1;
    DateGrid1.Items.Refresh();