I have a WPF (.NET 4) DataGrid backed by a DataView from a DataTable, which has as one of its columns a DataGridComboBoxColumn. Everything is working for me, but I had to create a new class to hold the contents of what is a simple [ArrayList/List<string>/whatever IEnumerable I can try] for the possible values of the DGCBC, when it seems like there should be a less-convoluted way. Here is my XAML:
<Window x:Class="MASTableMaint.TruckloadLimitsEdit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Edit Truckload Limits" Height="600" Width="460" MinWidth="300" MinHeight="300" WindowStartupLocation="CenterOwner" >
<DockPanel LastChildFill="True">
<DataGrid Name="TruckloadGrid" ItemsSource="{Binding}" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="False" RowHeaderWidth="20">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="companyIdColumn" Header="Company" SelectedValueBinding="{Binding Path=CompanyID}"
DisplayMemberPath="TheCompanyID" SelectedValuePath="TheCompanyID"/>
...
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>
And my code behind:
namespace MASTableMaint
{
public partial class TruckloadLimitsEdit : Window
{
private DataTable truckloadDT = new DataTable();
private SqlDataAdapter truckloadDA;
private SqlConnection sqlconn;
public TruckloadLimitsEdit(List<string> massCompanies)
{
sqlconn = new SqlConnection(Constants.SqlConnectionStr);
if (!DbUtils.OpenDbConn(sqlconn))
{
sqlconn.Dispose();
return;
}
string query = "SELECT * FROM tTruckload ORDER BY CompanyID";
truckloadDA = new SqlDataAdapter(query, sqlconn);
truckloadDA.Fill(truckloadDT);
SqlCommandBuilder builder = new SqlCommandBuilder(truckloadDA);
InitializeComponent();
DataView truckloadDV = new DataView(truckloadDT, null, null, DataViewRowState.CurrentRows);
TruckloadGrid.ItemsSource = truckloadDV;
ObservableCollection<MassCompany> ocMassCompanies = new ObservableCollection<MassCompany>();
foreach (string company in massCompanies)
{
ocMasCompanies.Add(new MassCompany(company));
}
companyIdColumn.ItemsSource = ocMassCompanies;
}
...
}
public class MassCompany
{
public MassCompany(string company)
{
TheCompanyID = company;
}
public string TheCompanyID { get; set; }
}
}
It seems that the collection is shared for all rows.
If so, define a dependency property of type ObservableCollection<MassCompany> on your window (we'll call it WinMassCompanies).
Then in constructor:
WinMassCompanies = new ObservableCollection<MassCompany>();
foreach (string company in massCompanies)
{
WinMassCompanies.Add(new MassCompany(company));
}
//don't fiddle with the column
In XAML:
<DataGridComboBoxColumn ItemsSource="{Binding WinMassCompanies,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}}}"
...../>
Related
Using the MVVM pattern in a WPF application, I want to set on the 'Editing State' of a records in row.
Every time the user starts editing a record by clicking on Edit button, that row should switch to the 'editing' mode.
Finished, he can save all changes in the row by clicking the same or another button
How can I set edit mode (IsReadOnly=true/false) for All cells in selected Row on click "Edit" button?
Any help is appreciated!
This is my current code:
XAML
<Window x:Class="TotalRows.TotalRowsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TotalRows"
mc:Ignorable="d"
x:Name="xMainWindow"
Title="RowsTotalWindow" Height="450" Width="800">
<Window.DataContext>
<local:ExampleData/>
</Window.DataContext>
<Grid>
<StackPanel >
<DataGrid x:Name="myGrid" IsReadOnly="True" CanUserAddRows="False" SelectionMode="Single" CanUserDeleteRows="False"
ItemsSource="{Binding ItemsViewCollection}" RowDetailsVisibilityMode="Collapsed"
SelectedItem="{Binding SelectedItemRow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Stretch">
<ToggleButton x:Name="btnEditItem" Content="Edit" Width="50" Height="20" Margin="0 0 3 0"
Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.UpdateItemCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=DataContext}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="gr" Binding="{Binding Group}" Header="Gr" Width="30" />
<DataGridTextColumn x:Name="one" Binding="{Binding Col_1}" Header="h1" Width="30" />
<DataGridTextColumn x:Name="two" Binding="{Binding Col_2}" Header="h2" Width="30" />
<DataGridTextColumn x:Name="tree" Binding="{Binding Col_3}" Header="h3" Width="30" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</Window>
Code Behind
namespace TotalRows
{
public class ItemClass
{
public int Group { get; set; }
public string Title { get; set; }
public int Col_1 { get; set; }
public int Col_2 { get; set; }
public int Col_3 { get; set; }
}
public class ExampleData
{
private bool _IsReadMode;
public bool IsReadMode
{
get { return _IsReadMode; }
set
{
_IsReadMode = value;
OnPropertyChanged(nameof(IsReadMode));
}
}
private ItemClass _selectedItem = null;
public ItemClass SelectedItemRow
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged(nameof(SelectedItemRow));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<ItemClass> _items;
public ObservableCollection<ItemClass> Items
{
get
{
return _items;
}
set
{
if (_items != value)
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
}
private ICollectionView _itemsViewCollection;
public ICollectionView ItemsViewCollection
{
get
{
return _itemsViewCollection;
}
set
{
if (_itemsViewCollection != value)
{
_itemsViewCollection = value;
OnPropertyChanged(nameof(ItemsViewCollection));
}
}
}
public ICommand UpdateItemCommand { get; private set; }
public ExampleData()
{
IsReadMode = true;
UpdateItemCommand = new ViewModelCommand(param => updateItemCommand(param));
Items = new ObservableCollection<ItemClass>()
{
new ItemClass() {Group=1, Title="Item1", Col_1=100, Col_2=150, Col_3=250},
new ItemClass() {Group=2, Title="Item1", Col_1=50, Col_2=2, Col_3=200},
new ItemClass() {Group=2, Title="Item2", Col_1=50, Col_2=100, Col_3=40},
new ItemClass() {Group=3, Title="Item1", Col_1=60, Col_2=25, Col_3=230},
new ItemClass() {Group=3, Title="Item2", Col_1=30, Col_2=25, Col_3=0},
new ItemClass() {Group=3, Title="Item3", Col_1=9, Col_2=100, Col_3=20},
new ItemClass() {Group=4, Title="Item1", Col_1=46, Col_2=32, Col_3=30},
};
ItemsViewCollection = CollectionViewSource.GetDefaultView(Items);
ItemsViewCollection.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
}
private void updateItemCommand(object param)
{
IsReadMode = !IsReadMode;
}
}
}
Do you realise f2 switches the current row into edit mode?
CommandManager.RegisterClassInputBinding(ownerType, new InputBinding(BeginEditCommand, new KeyGesture(Key.F2)));
CommandManager.RegisterClassCommandBinding(ownerType, new CommandBinding(BeginEditCommand, new ExecutedRoutedEventHandler(OnExecutedBeginEdit), new CanExecuteRoutedEventHandler(OnCanExecuteBeginEdit)));
You could bind your edit button to a command in the ExampleData viewmodel and pass a reference to the specific ItemClass as a command parameter.
Use relativesource binding to get to that command.
ExampleData owns that collection so you can set properties on that instance and stash a reference or index to the last one they edited set the flag back. Or iterate through the whole collection.
Seems you know how to write a command but I recommend the community mvvm toolkit and relaycommand.
Your binding would be something like
<Button Command="{Binding DataContext.EditThisOneCommand
, RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="{Binding}">
The command parameter passes the row to an Icommand so will be a parameter passed to your command.
A similar command I happen to have.
private RelayCommand<Thing> _colourCommand;
public RelayCommand<Thing> ColourCommand
{
get
{
return _colourCommand
?? (_colourCommand = new RelayCommand<Thing>(
_thing =>
{
_thing.Row = Items.IndexOf(_thing);
},
_thing => CanUserClick));
}
}
You would of course have EditThisOneCommand
You then have to tell the UI to issue a BeginEditCommand.
And then you need to tell the UI to issue a CommitEditCommand when you finish.
CommandManager.RegisterClassCommandBinding(ownerType, new CommandBinding(CommitEditCommand, new ExecutedRoutedEventHandler(OnExecutedCommitEdit), new CanExecuteRoutedEventHandler(OnCanExecuteCommitEdit)));
These commands are source from the datagrid.
You could instead just bind those datagrid commands to buttons and not have this flag.
A datagridrow has a property IsEditing. You might be able to bind that onewaytosource to your flag. You'd set that binding via a style.
Not sure why you'd want to, but you could take a look at that.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagridrow.isediting?view=windowsdesktop-7.0
I've created a FlowDocument with databinded Datagrid.
Output in FlowDocumentScrollviewer is like this:
Next, I need to convert this FlowDocument into Fixed document (XPS), in order to print It in A4 paper size. But once I do that, my DataGrid data is lost.
Here is my XAML:
<Window x:Class="Test_Flow.Izpisi.DataGrid_DataTable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test_Flow.Izpisi"
mc:Ignorable="d"
Title="DataGrid_DataTable" Height="850" Width="850"
WindowStartupLocation="CenterScreen">
<Grid>
<FlowDocumentScrollViewer Name="Flow_reader" >
<FlowDocument Name="Flow_dokument" PageHeight="29.7cm" PageWidth="21cm" >
<Paragraph>Datagrid with DataTable example</Paragraph>
<BlockUIContainer>
<DataGrid BorderThickness="1" ItemsSource="{Binding Dt_Flow}" IsEnabled="False"
AutoGenerateColumns="False" HeadersVisibility="Column" BorderBrush="Black">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Number}"/>
<DataGridTextColumn Header="Surname" Binding="{Binding Surname}"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}"/>
<DataGridTextColumn Header="City" Binding="{Binding City}"/>
</DataGrid.Columns>
</DataGrid>
</BlockUIContainer>
</FlowDocument>
</FlowDocumentScrollViewer>
<DocumentViewer Name="doc_viewer" Visibility="Collapsed" />
</Grid>
</Window>
And my View_Model:
class My_ViewModel : INotifyPropertyChanged
{
public My_ViewModel(DocumentViewer doc_viewer, FlowDocument dokument, FlowDocumentScrollViewer page_viewer)
{
Fill_Table(); //Fill some test data
//Convert to XPS and display It in DocumentViewer
Convert_to_XPS(doc_viewer, dokument, page_viewer);
}
public DataTable Dt_Flow { get; set; } //DataTable for FlowDocument
private void Convert_to_XPS(DocumentViewer doc_viewer, FlowDocument dokument, FlowDocumentScrollViewer page_viewer)
{
//Convert FlowDocument to XPS
MemoryStream ms = new MemoryStream();
Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
string pack = "pack://report.xps";
PackageStore.RemovePackage(new Uri(pack));
PackageStore.AddPackage(new Uri(pack), pkg);
XpsDocument doc = new XpsDocument(pkg, CompressionOption.Maximum, pack);
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(doc), false);
DocumentPaginator paginator = ((IDocumentPaginatorSource)dokument).DocumentPaginator;
rsm.SaveAsXaml(paginator);
//Hide FlowDocumentScrollViwer and show DocumentViewer
page_viewer.Visibility = Visibility.Collapsed;
doc_viewer.Document = doc.GetFixedDocumentSequence();
doc_viewer.Visibility = Visibility.Visible;
}
private void Fill_table()
{
//Fill test table
Dt_Flow = new DataTable();
Dt_Flow.Columns.Add("Number");
Dt_Flow.Columns.Add("Surname");
Dt_Flow.Columns.Add("Address");
Dt_Flow.Columns.Add("City");
int my_number = 1;
for (int i = 0; i < 10; i++)
{
DataRow _newRow = Dt_Flow.NewRow();
_newRow["Number"] = my_number + ".";
_newRow["Surname"] = "Johnson";
_newRow["Address"] = "Beverly Hills";
_newRow["City"] = "Los Angeles";
Dt_Flow.Rows.Add(_newRow);
my_number++;
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Why is that happening, how can I fix this ?
EDIT: I added "Fill_table" method in "My_ViewMmodel", so you can test whole thing by yourself.
I am learning about the data binding especially with DataGrid. In my code here, I have a DataGrid and a Labelwhich shows the first cell value of DataGrid. Output of XAML is like . Considering the image below, The Label content next to The First Cell Value is: Monkey which I think i have got from the DataGrid first cell. Now what I wanted was to update left of The First Cell Value is: when I change the value in my DataGrid first cell. But I am unable to achieve it.
Bellow is my Code and the XAML File
CODE
namespace DataGridExampleSelfTry
{
public class MainWindowVM:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
private string _FirstCell;
public string FirstCell
{
get{ return _FirstCell; }
set
{
_FirstCell = value;
PropertyChanged(this,new PropertyChangedEventArgs(nameof(FirstCell)));
}
}
public string SecondCell { get; set; }
private ObservableCollection<animies> _animelistforbinding;
public ObservableCollection<animies> animelistforbinding
{ get
{
return _animelistforbinding;
}
set
{
_animelistforbinding = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(animelistforbinding)));
}
}
ObservableCollection<animies> addinganime = new ObservableCollection<animies>();
public MainWindowVM()
{
addinganime.Add(new animies("Monkey", "D Luffy"));
animelistforbinding = addinganime;
FirstCell = animelistforbinding[0].FirstName;
SecondCell = animelistforbinding[0].LastName;
}
}
public class animies:INotifyPropertyChanged
{
private string _FirstName;
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(FirstName)));
}
}
public string LastName { get; set; }
public animies(string dFirstName, string dLastName)
{
FirstName = dFirstName;
LastName = dLastName;
}
}
}
XAML
<Window x:Class="DataGridExampleSelfTry.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridExampleSelfTry"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="450">
<Window.DataContext>
<local:MainWindowVM/>
</Window.DataContext>
<StackPanel>
<DataGrid x:Name="XAML_DataGrid"
AutoGenerateColumns="False" CanUserAddRows="False"
ItemsSource="{Binding animelistforbinding}" Margin="5"
CanUserSortColumns="False" HorizontalGridLinesBrush="Gray"
VerticalGridLinesBrush="Gray" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Header="First name" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" Width="*" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal">
<Label Content="The First Cell Value is : "/>
<Label Content="{ Binding FirstCell}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="The Second Cell Value is : "/>
<Label Content="{ Binding SecondCell}"/>
</StackPanel>
<Button Content="Button" Margin="50"/>
</StackPanel>
</Window>
Thank you for your Help.
Either bind to directly to the same property that the first column of the DataGrid binds to:
<Label Content="{Binding animelistforbinding[0].FirstName}"/>
...or set the FirstCell property whenever the FirstName property of the first item in animelistforbinding is set. You can do this by handling the PropertyChanged event for the first item in your view model:
public MainWindowVM()
{
addinganime.Add(new animies("Monkey", "D Luffy"));
animelistforbinding = addinganime;
FirstCell = animelistforbinding[0].FirstName;
SecondCell = animelistforbinding[0].LastName;
animelistforbinding[0].PropertyChanged += (s, e) => FirstCell = animelistforbinding[0].FirstName;
}
Do you know why looking at the code in the XAML and ViewModel why a Data Grid wont update when the an item is added? I know the DataGrid binding is correct as will add an item if I add it by code, however I am adding a item via a view.
Also the getter of the ObservableCollection is getting hit every time I do add a item.
Thanks
------ Code ------
XAML
<Window x:Class="Importer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Product Group" Height="350" Width="721"
xmlns:VM="clr-namespace:Importer.ViewModels">
<Window.DataContext>
<VM:ProductsViewModel />
</Window.DataContext>
<Grid Margin="0,0,0.4,0.4">
<DataGrid x:Name="dgGrid" ColumnWidth="Auto" Margin="10,10,0,0" VerticalAlignment="Top"
ItemsSource="{Binding ProductCollection}" HorizontalAlignment="Left" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="150"/>
<DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="400" />
</DataGrid.Columns>
</DataGrid>
<Button x:Name="btnAddProjectGroup" Content="Add Project Group" HorizontalAlignment="Left" Margin="10,277,0,0" VerticalAlignment="Top" Width="135" Click="btnAddProjectGroup_Click"/>
</Grid>
</Window>
VIEW 1 (that uses the XAML above)
public partial class MainWindow : Window
{
ProductsViewModel m_VM = new ProductsViewModel();
public MainWindow()
{
InitializeComponent();
}
private void btnAddProjectGroup_Click(object sender, RoutedEventArgs e)
{
// Open new window here to add a new project group
AddProductGroup dlg = new AddProductGroup(m_VM);
dlg.ShowDialog();
}
}
VIEW 2 - The view that passes the values to add to the collection
public partial class AddProductGroup : Window
{
ProductGroupBindable newProduct = new ProductGroupBindable();
ProductsViewModel viewModel = null;
public AddProductGroup(ProductsViewModel vm)
{
viewModel = vm;
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
newProduct.Id = System.Guid.NewGuid();
newProduct.Name = txtName.Text;
newProduct.Description = txtDescription.Text;
viewModel.AddProduct(newProduct);
this.Close();
}
}
VIEWMODEL
private ObservableCollection<ProductGroupBindable> m_ProductCollection;
public ProductsViewModel()
{
if (m_ProductCollection == null)
m_ProductCollection = new ObservableCollection<ProductGroupBindable>();
}
public ObservableCollection<ProductGroupBindable> ProductCollection
{
get
{
return m_ProductCollection;
}
set
{
m_ProductCollection = value;
}
}
private ObservableCollection<ProductGroupBindable> Test()
{
m_ProductCollection.Add(new ProductGroupBindable { Description = "etc", Name = "test12" });
m_ProductCollection.Add(new ProductGroupBindable { Description = "etc", Name = "test123" });
return ProductCollection;
}
public void AddProduct(ProductGroupBindable newProduct)
{
m_ProductCollection.Add(newProduct);
//NotifyPropertyChanged("ProductCollection");
}
You set DataContext in XAML:
<Window.DataContext>
<VM:ProductsViewModel />
</Window.DataContext>
and your dialog works on another instance of ProductsViewModel which you create in code:
ProductsViewModel m_VM = new ProductsViewModel();
Easiest workaround would be to pass DataContext to your AddProductGroup dialog:
AddProductGroup dlg = new AddProductGroup(this.DataContext as ProductsViewModel);
EDIT
or instead of setting DataContext in XAML do set it in code:
public MainWindow()
{
InitializeComponent();
this.DataContext = m_VM;
}
I have tried everything and got nowhere so I'm hoping someone can give me the aha moment.
I simply cannot get the binding to pull the data in the datagrid successfully.
I have a DataTable that contains multiple columns with of MyDataType
public class MyData
{
string nameData {get;set;}
bool showData {get;set;}
}
MyDataType has 2 properties (A string, a boolean)
I have created a test DataTable
DataTable GetDummyData()
{
DataTable dt = new DataTable("Foo");
dt.Columns.Add(new DataColumn("AnotherColumn", typeof(MyData)));
dt.Rows.Add(new MyData("Row1C1", true));
dt.Rows.Add(new MyData("Row2C1", false));
dt.AcceptChanges();
return dt;
}
I have a WPF DataGrid which I want to show my DataTable.
But all I want to do is to change how each cell is rendered to show [TextBlock][Button] per cell with values bound to the MyData object and this is where I'm having a tonne of trouble.
My XAML looks like this
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="MyDataTemplate" DataType="MyData">
<StackPanel Orientation="Horizontal" >
<Button Background="Green" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5,0,0,0" Content="{Binding Path=nameData}"></Button>
<TextBlock Background="Green" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding Path=nameData}"></TextBlock>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<dg:DataGrid Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="True"
x:Name="dataGrid1" SelectionMode="Single" CanUserAddRows="False"
CanUserSortColumns="true" CanUserDeleteRows="False" AlternatingRowBackground="AliceBlue"
AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" />
</Grid>
Now all I do once loaded is to attempt to bind the DataTable to the WPF DataGrid
dt = GetDummyData();
dataGrid1.ItemsSource = dt.DefaultView;
The TextBlock and Button show up, but they don't bind, which leaves them blank.
Could anyone let me know if they have any idea how to fix this.
This should be simple, thats what Microsoft leads us to believe.
I have set the Column.CellTemplate during the AutoGenerating event and still get no binding.
Please help!!!
Edit: Updated to reflect the input of Aran Mulholland (see comment)
Apparently the DataGrid is passing the entire DataRowView to each cell. That's why the binding doesn't work. Your DataTemplate expects the DataContext to be of type MyData, but instead it is of type DataRowView. My proposed (somewhat hack-ish) workaround to get the DataContext you want is to create a custom DataGridTemplateColumn that will extract the necessary item from the DataRowView. The code is below:
<Window x:Class="DataGridTemplateColumnSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="MyDataTemplate" DataType="DataRowView">
<StackPanel Orientation="Horizontal">
<Button Background="Green" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5,0,0,0" Content="{Binding Path=nameData}"></Button>
<TextBlock Background="Green" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding Path=nameData}"></TextBlock>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<dg:DataGrid Grid.Row="1" AutoGenerateColumns="True" x:Name="dataGrid1" SelectionMode="Single"
CanUserAddRows="False" CanUserSortColumns="true" CanUserDeleteRows="False"
AlternatingRowBackground="AliceBlue" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn"
ItemsSource="{Binding}" VirtualizingStackPanel.VirtualizationMode="Standard" />
</Grid>
</Window>
using System.Data;
using System.Windows;
using Microsoft.Windows.Controls;
namespace DataGridTemplateColumnSample
{
public partial class Window1
{
public Window1()
{
InitializeComponent();
DataContext = GetDummyData().DefaultView;
}
private static DataTable GetDummyData()
{
var dt = new DataTable("Foo");
dt.Columns.Add(new DataColumn("OneColumn", typeof(MyData)));
dt.Columns.Add(new DataColumn("AnotherColumn", typeof(MyData)));
dt.Rows.Add(new MyData("Row1C1", true), new MyData("Row1C2", true));
dt.Rows.Add(new MyData("Row2C1", false), new MyData("Row2C2", true));
dt.AcceptChanges();
return dt;
}
private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var column = new DataRowColumn(e.PropertyName);
column.Header = e.Column.Header;
column.CellTemplate = (DataTemplate)Resources["MyDataTemplate"];
e.Column = column;
}
}
public class DataRowColumn : DataGridTemplateColumn
{
public DataRowColumn(string column) { ColumnName = column; }
public string ColumnName { get; private set; }
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var row = (DataRowView) dataItem;
var item = row[ColumnName];
cell.DataContext = item;
var element = base.GenerateElement(cell, item);
return element;
}
}
public class MyData
{
public MyData(string name, bool data) { nameData = name; showData = data; }
public string nameData { get; set; }
public bool showData { get; set; }
}
}
Note: This approach only appears to work with container virtualization off or in Standard mode. If the VirtualizationMode is set to Recycling the template is not applied.
After finding this thread and having trouble with the code shown here, I ran across this thread on MSDN, and it works much better! No virtualization problems at all so far as I've seen.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062
Code:
private void dataGrid1_AutoGeneratingColumn(object sender, Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(MyData))
{
MyDataGridTemplateColumn col = new MyDataGridTemplateColumn();
col.ColumnName = e.PropertyName; // so it knows from which column to get MyData
col.CellTemplate = (DataTemplate)FindResource("MyDataTemplate");
e.Column = col;
e.Column.Header = e.PropertyName;
}
}
public class MyDataGridTemplateColumn : DataGridTemplateColumn
{
public string ColumnName
{
get;
set;
}
protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
// The DataGridTemplateColumn uses ContentPresenter with your DataTemplate.
ContentPresenter cp = (ContentPresenter)base.GenerateElement(cell, dataItem);
// Reset the Binding to the specific column. The default binding is to the DataRowView.
BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding(this.ColumnName));
return cp;
}
}