The viky-related answer summarizes the idea of adding a button column from a source. I put together a short example that also shows the second part of your question, which is how to enable / disable a button based on data in the grid. I used DataGrid in 4.0, but the toolbox with 3.5 should also be great.
First, I add one Name column from XAML. This is optional, but just shows that you can combine the XAML and C # add-ons with the grid. Note that I call the grid here, so I have a way to reference it in a partial C # class.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Data Grid Populated in XAML and C#">
<Grid>
<DataGrid x:Name="_gridControl" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
. ( ), , true, "" false. , ( ), , , ItemControl , IsEnabled, .
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var buttonTemplate = new FrameworkElementFactory(typeof(Button));
buttonTemplate.SetBinding(Button.ContentProperty, new Binding("Name"));
buttonTemplate.SetBinding(Button.IsEnabledProperty, new Binding("Status")
{
Converter = new StatusToEnabledConverter()
});
buttonTemplate.AddHandler(
Button.ClickEvent,
new RoutedEventHandler((o, e) => MessageBox.Show("hi"))
);
this._gridControl.Columns.Add(
new DataGridTemplateColumn()
{
Header = "Close Button",
CellTemplate = new DataTemplate() { VisualTree = buttonTemplate }
}
);
this._gridControl.ItemsSource = new object[]
{
new { Name = "First Item", Status = "Open" },
new { Name = "Second Item", Status = "Open" },
new { Name = "Third Item", Status = "Closed" },
new { Name = "Fourth Item", Status = "Closed" },
new { Name = "Fifth Item", Status = "Open" }
};
}
}
public class StatusToEnabledConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
return "Open".Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
, , DataGrid # InitializeComponent. WPF, XAML #, , , XAML, , , XAML.
, !