Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate DataGridTemplateColumn Through C# Code
    primarykey
    data
    text
    <p>I have a dynamic Datagrid that I have created. I am creating each column for it through code behind. I am having troubles on a column that I want to be displayed at a textblock when not editing, but as a combobox while editing. I have an ObservableCollection of Transactions. Each Transaction has a type called "Account". Here is what I have so far:</p> <pre><code> private DataGridTemplateColumn GetAccountColumn() { // Create The Column DataGridTemplateColumn accountColumn = new DataGridTemplateColumn(); accountColumn.Header = "Account"; Binding bind = new Binding("Account"); bind.Mode = BindingMode.TwoWay; // Create the TextBlock FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock)); textFactory.SetBinding(TextBlock.TextProperty, bind); DataTemplate textTemplate = new DataTemplate(); textTemplate.VisualTree = textFactory; // Create the ComboBox bind.Mode = BindingMode.OneWay; FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox)); comboFactory.SetValue(ComboBox.DataContextProperty, this.Transactions); comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true); comboFactory.SetBinding(ComboBox.ItemsSourceProperty, bind); DataTemplate comboTemplate = new DataTemplate(); comboTemplate.VisualTree = comboFactory; // Set the Templates to the Column accountColumn.CellTemplate = textTemplate; accountColumn.CellEditingTemplate = comboTemplate; return accountColumn; } </code></pre> <p>The value displays in the TextBlock. However, in the combobox, I am only getting one character to display per item. For example, here is the textblock:</p> <p><img src="https://i.stack.imgur.com/M0Ind.jpg" alt="enter image description here"></p> <p>But when I click to edit and go into the combobox, here is what is shown:</p> <p><img src="https://i.stack.imgur.com/EXy0W.jpg" alt="enter image description here"></p> <p>Can someone help me out so that the items in the Combobox are displayed properly? Also, when I select something from the Combobox, the textblock isn't updated with the item I selected.</p> <p>UPDATED:</p> <p>Here is my column as of now. The items in the ComboBox are being displayed properly. The issue now is that when a new item is selected, the text in the TextBlock isn't updated with the new item.</p> <pre><code> private DataGridTemplateColumn GetAccountColumn() { // Create The Column DataGridTemplateColumn accountColumn = new DataGridTemplateColumn(); accountColumn.Header = "Account"; Binding bind = new Binding("Account"); bind.Mode = BindingMode.OneWay; // Create the TextBlock FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock)); textFactory.SetBinding(TextBlock.TextProperty, bind); DataTemplate textTemplate = new DataTemplate(); textTemplate.VisualTree = textFactory; // Create the ComboBox Binding comboBind = new Binding("Account"); comboBind.Mode = BindingMode.OneWay; FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox)); comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true); comboFactory.SetValue(ComboBox.ItemsSourceProperty, this.Accounts); comboFactory.SetBinding(ComboBox.SelectedItemProperty, comboBind); DataTemplate comboTemplate = new DataTemplate(); comboTemplate.VisualTree = comboFactory; // Set the Templates to the Column accountColumn.CellTemplate = textTemplate; accountColumn.CellEditingTemplate = comboTemplate; return accountColumn; } </code></pre> <p>The "Accounts" property is declared like this in my MainWindow class:</p> <pre><code>public ObservableCollection&lt;string&gt; Accounts { get; set; } public MainWindow() { this.Types = new ObservableCollection&lt;string&gt;(); this.Parents = new ObservableCollection&lt;string&gt;(); this.Transactions = new ObservableCollection&lt;Transaction&gt;(); this.Accounts = new ObservableCollection&lt;string&gt;(); OpenDatabase(); InitializeComponent(); } </code></pre> <p>Here is my Transaction Class:</p> <pre><code>public class Transaction { private string date; private string number; private string account; public string Date { get { return date; } set { date = value; } } public string Number { get { return number; } set { number = value; } } public string Account { get { return account; } set { account = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload