2012-01-08 17 views
14

私が作成した動的なDatagridがあります。私はコードの背後にそれを使って各列を作成しています。私は、編集していないときはテキストブロックに、編集中はコンボボックスとして表示したいというコラムには問題があります。 ObservableCollection of Transactionsがあります。各取引には「勘定」というタイプがあります。ここに私がこれまで持っているものがあります:C#コードによるDataGridTemplateColumnの作成

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

値はTextBlockに表示されます。しかし、コンボボックスでは、アイテムごとに表示するキャラクタが1つしかありません。誰かがそう私を助けることができる

enter image description here

enter image description here

しかし、私が編集して、コンボボックスに行くためにクリックしたときに、ここで示されているものである:例えば、ここでのテキストブロックがありますコンボボックスのアイテムが正しく表示されていることを確認しますか?また、コンボボックスから何かを選択すると、選択したアイテムでテキストブロックが更新されません。

更新日:

ここは現在のカラムです。 ComboBox内の項目が正しく表示されています。問題は、新しい項目が選択されると、TextBlock内のテキストが新しい項目で更新されないということです。

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

"アカウント" プロパティは、私のメイン・ウィンドウクラスで、次のように宣言されています。

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(); 
    } 

は、ここに私のトランザクションクラスです:あなたはItemsSource選択した値にバインド

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

異なるバインディングに同じバインディングインスタンスを再使用するべきではありません... –

答えて

3

、文字列、別名char配列なので、すべての文字が項目として使用されるため、ItemsSourceバインディングは、おそらく値がchオッセン

+0

ああ、それは理にかなっています!情報をありがとう!私はitemssourceが別のコレクションから来るようにコードを追加しました。コンボボックス内の項目は、必要に応じて表示されます。もう1つのことですが、コンボボックスから別の値を選択すると、そのテキストブロックは新しいアイテムで更新されません。どのように私はそれを修正するだろうか? –

+0

@EricR .: 'SelectedItem'または' SelectedValue'を 'Account'にバインドします。アイテムが既にアカウントの値として使用される文字列である場合は 'SelectedItem'を使用し、[' SelectedValue']を使用します(http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives .selector.selectedvalue.aspx)を['SelectedValuePath'](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx)と組み合わせて使用​​します。選択した項目のプロパティ(項目自体ではありません)。 –

+0

どうすればいいですか?私が使用しているアカウントの新しいコレクションはObservableCollection です。私は今このItemsSourceを設定しています:comboFactory.SetValue(ComboBox.ItemsSourceProperty、this.Accounts);. this.AccountsはObservableCollectionです。では、SelectedItemをどのように設定するのですか?私はあなたが今まで私に与えてくれたすべての助けに本当に感謝しています! –

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) 

バインディングを作成することにより、WPFのデータグリッドにSelectedValueを設定することができます。

関連する問題