Tôi có Bảng dữ liệu động mà tôi đã tạo. Tôi đang tạo từng cột cho nó thông qua mã phía sau. Tôi đang gặp rắc rối trên một cột mà tôi muốn được hiển thị tại một textblock khi không chỉnh sửa, nhưng như là một combobox trong khi chỉnh sửa. Tôi có một ObservableCollection của giao dịch. Mỗi Giao dịch có một loại được gọi là "Tài khoản". Dưới đây là những gì tôi có cho đến thời điểm này:Tạo DataGridTemplateColumn Thông qua Mã C#
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;
}
Giá trị hiển thị trong TextBlock. Tuy nhiên, trong combobox, tôi chỉ nhận được một ký tự để hiển thị cho mỗi mục. Ví dụ, đây là TextBlock:
Nhưng khi tôi bấm vào để chỉnh sửa và đi vào combobox, đây là những gì được thể hiện:
Ai đó có thể giúp tôi ra để rằng các mục trong Combobox được hiển thị đúng cách? Ngoài ra, khi tôi chọn một cái gì đó từ Combobox, textblock không được cập nhật với mục tôi đã chọn.
CẬP NHẬT:
Đây là cột của tôi như bây giờ. Các mục trong ComboBox đang được hiển thị đúng cách. Vấn đề bây giờ là khi một mục mới được chọn, văn bản trong TextBlock không được cập nhật với mục mới.
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;
}
"Tài khoản" tài sản được khai báo như thế này trong lớp MainWindow tôi:
public ObservableCollection<string> Accounts { get; set; }
public MainWindow()
{
this.Types = new ObservableCollection<string>();
this.Parents = new ObservableCollection<string>();
this.Transactions = new ObservableCollection<Transaction>();
this.Accounts = new ObservableCollection<string>();
OpenDatabase();
InitializeComponent();
}
Đây là lớp giao dịch của tôi:
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; }
}
}
Bạn không nên sử dụng lại cùng một cá thể ràng buộc cho các ràng buộc khác nhau ... –