2017-08-11 10 views
0

を経由してコンボボックスの列にデータグリッドの列を設定しますコンボボックスを表示するテンプレート。は動的に私は「GridCollection」と呼ばれてきたDataTableにバインドさデータグリッドを持っているデータバインディング

私は、DataTable列( "Caption")のプロパティにバインドされたDataTriggerを追加し、それに応じてコンボボックスの可視性を更新することでこれを達成しようとしました。

私の理解は、この場合のDataContextはDataTableの列です。

私はDataTrigger(「ShowComboBox」)にspeicified値と一致するように、データテーブルの列の「キャプション」プロパティを設定しようとしている:このコード行が実行されていることを確認した後、その

ViewModel.GridCollection.Columns[index].Caption = "ShowComboBox"; 

Captionプロパティが更新されていますが、これはDataGridに変更を加えません。コンボボックスを表示している列はありません。

バインディングに問題があるか、それが他のものかどうかはわかりません。

ご協力いただきましてありがとうございます。

答えて

1

INotifyPropertyChangedインタフェースを実装していませんプロパティの変更通知をWPFに呼び出すと、これは機能しません。

あなたはしかしDataTableに列を追加し、この1に結合している場合、それは動作します:

ViewModel.GridCollection.Columns.Add(new DataColumn("Caption")); 

違いはDataRowViewクラスがINotifyPropertyChangedインタフェースを実装していることです。これは、WPFがソースプロパティの変更をリッスンし、ビューを動的に更新できるようにするために必要です。

+0

ありがとうございます。私は、DataGridの残りの部分が正しく更新されているので、DataColumnがINotifyPropertyChangedを実装していると仮定しました。私はDataTableのように思えます。カラム名はファイルから読み込まれ、実行時までコンボボックスのカラムを知る方法はありません。列の名前やインデックスを知らなくても、このバインディングをXAMLに書き込む方法はありますか? –

+0

なぜバインディングを参照していますか?どの部分が動的なのですか?私は私の答えで示唆したように、あなたは動的にCaption列を追加できませんか? – mm8

+0

列ヘッダーは動的です(実際は、テーブル全体がファイルから読み込まれます)。私のコードの "Caption"は、Columnのヘッダー名ではなく、個々のDataColumnのプロパティです。私はDataColumnのプロパティにバインドする必要があります。それは私が言及しているバインディングです。 –

0

レイアウトを変更する場合は、バインドをリセットする必要があります。 バインド/データソースをDataGridから削除します。キャプションの値を変更し、ソースを再度データグリッドにバインドします。ここで

私の作品コード:窓

Window x:Class="WpfApplication7.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:WpfApplication7" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <DataGrid Name="grd" AutoGenerateColumns="False" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=DataContext}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="01" Binding="{Binding Name}" /> 
      <DataGridTextColumn Header="02" Binding="{Binding LastName}" /> 
      <DataGridTemplateColumn Header="CBX"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox> 
          <ComboBox.Style> 
           <Style TargetType="ComboBox"> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding Path=Caption, Mode=TwoWay}" Value="ShowCombobox"> 
              <Setter Property="Visibility" Value="Collapsed" /> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </ComboBox.Style> 
         </ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Content="Click" Click="Button_Click" Grid.Row="1"/> 
</Grid> 
</Window> 

XAMLの背後にあるコードは:DataColumn以来

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication7 
{ 
/// <summary> 
/// Interaktionslogik für MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 

     items soureitems = new items(); 

     for(int i = 0; i <= 10; i++) 
     { 
      item sourceitem = new item(); 
      sourceitem.Name = "John"; 
      sourceitem.LastName = "Doe"; 
      sourceitem.Caption = ""; 
      soureitems.Add(sourceitem); 
     } 

     this.DataContext = soureitems; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 

     items sourceitems = (items)(this.DataContext); 
     this.DataContext = null; 
     foreach (item sourceitem in sourceitems) 
     { 
      if(sourceitem.Caption != "ShowCombobox") 
      { 
       sourceitem.Caption = "ShowCombobox"; 
      } else 
      { 
       sourceitem.Caption = ""; 
      } 
     } 
     this.DataContext = sourceitems; 
    } 
} 

public class items : System.Collections.ObjectModel.ObservableCollection<item> { } 

public class item 
{ 
    string _Name; 
    string _LastName; 
    string _Caption; 
    public string Name { 
     get { return _Name; } 
     set { _Name = value; } 
    } 
    public string LastName 
    { 
     get { return _LastName; } 
     set { _LastName = value; } 
    } 
    public string Caption 
    { 
     get { return _Caption; } 
     set { _Caption = value; } 
    } 
} 
} 
関連する問題