2012-01-08 43 views
14

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:

enter image description here

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:

enter image description here

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; } 
    } 
} 
+0

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 ... –

Trả lời

3

Bạn ràng buộc ItemsSource với giá trị được lựa chọn, một chuỗi, hay còn gọi là mảng char, vì vậy mỗi ký tự được sử dụng như một mục, ràng buộc ItemsSource có lẽ nên nhắm mục tiêu một số bộ sưu tập khác mà từ đó giá trị có thể được ch osen.

+0

Ahhh vâng, điều đó có ý nghĩa! Cảm ơn bạn về thông tin! Tôi đã thêm mã để các mục nguồn đến từ một bộ sưu tập khác ngay bây giờ. Các mục trong combobox xuất hiện như chúng cần. Mặc dù vậy, một điều nữa, khi tôi chọn một giá trị khác từ combobox, textblock không được cập nhật với mục mới. Làm thế nào tôi sẽ sửa chữa điều đó? –

+0

@EricR .: Liên kết 'SelectedItem' hoặc' SelectedValue' thành 'Tài khoản'. Sử dụng 'SelectedItem' nếu các mục đã là các chuỗi được sử dụng làm giá trị cho tài khoản, sử dụng [' SelectedValue'] (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives .selector.selectedvalue.aspx) kết hợp với ['SelectedValuePath'] (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx) nếu giá trị là một thuộc tính trên mục đã chọn (và không phải là chính mục đó). –

+0

Tôi sẽ làm như thế nào? Bộ sưu tập mới cho Tài khoản tôi đang sử dụng là ObservableCollection . Tôi đang thiết lập ItemsSource ngay bây giờ bởi điều này: comboFactory.SetValue (ComboBox.ItemsSourceProperty, this.Accounts) ;. this.Accounts là ObservableCollection. Vậy làm thế nào tôi sẽ thiết lập SelectedItem cho điều đó? Tôi thực sự đánh giá cao tất cả sự giúp đỡ bạn đã cho tôi cho đến nay! –

0
Dim newBind As Binding = New Binding("LinktoCommonOutputBus") 
newBind.Mode = BindingMode.OneWay 

factory1.SetValue(ComboBox.ItemsSourceProperty, dictionary) 
factory1.SetValue(ComboBox.NameProperty, name) 
factory1.SetValue(ComboBox.SelectedValuePathProperty, "Key") 
factory1.SetValue(ComboBox.DisplayMemberPathProperty, "Value") 
factory1.SetBinding(ComboBox.SelectedValueProperty, newBind) 

Bằng cách tạo Ràng buộc, bạn có thể đặt SelectedValue trong bảng dữ liệu cho WPF.