2016-04-21 11 views
0

WPF DataGridコントロールを実行時に生成する列があります。WPF DataGridフォアグラウンドで項目のプロパティにバインドするプログラムで

public class ExamineExamplesDataGridItem 
{ 
    public List<string> PredictiveAttributeValues { get; set; } 
    public string DecisiveAttributeValue { get; set; } 
    public string ExaminedAttributeValue { get; set; } 

    public Brush ExaminedAttributeValueColor 
    { 
     // if I set a debug point here, I'll get there, but it makes no sense 
     get 
     { 
      return DecisiveAttributeValue == null || ExaminedAttributeValue == null 
       ? Brushes.Black 
       : DecisiveAttributeValue.Equals(ExaminedAttributeValue) ? Brushes.Green : Brushes.Red; 
     } 
     set { } 
    } 
} 

DataGridインスタンスのフィールドを結合、すべてが着色以外正常に動作します:

List<ExamineExamplesDataGridItem> items = new List<ExamineExamplesDataGridItem>(); 

// several columns, works fine 
for (int i = 0; i < _attributeTypeSet.PredictiveAttributeTypes.Count; i++) 
{ 
    DataGridComboBoxColumn attributeTypeColumn = new DataGridComboBoxColumn() 
    { 
     TextBinding = new Binding("PredictiveAttributeValues[" + i + "]") 
    }; 
    dataGrid.Columns.Add(attributeTypeColumn); 
} 

// prev last column, works fine 
DataGridComboBoxColumn decisiveComboBoxColumn = new DataGridComboBoxColumn() 
{ 
    CellStyle = 
     new Style(typeof (DataGridCell)) {Setters = {new Setter() {Property = ForegroundProperty, Value = Brushes.Blue}}} 
}; 
dataGrid.Columns.Add(decisiveComboBoxColumn); 

// last column, which content I want to color 
DataGridTextColumn examinedColumn = new DataGridTextColumn() 
{ 
    ElementStyle = new Style() 
    { 
     TargetType = typeof(TextBlock), 
     Setters = 
     { 
      // this works, but.. 
      // new Setter(TextBlock.ForegroundProperty, Brushes.Red) 

      // ... I need this one. It calls ExaminedAttributeValueColor property, but displays black color anyway 
      new Setter(TextBlock.ForegroundProperty, new Binding("ExaminedAttributeValueColor")) 
     } 
    } 
}; 
dataGrid.Columns.Add(examinedColumn); 

dataGrid.ItemsSource = items; 

ItemsSourceその色を取り込む黒いままそのItemsSourceとして、私はExamineExampleDataGridItemのジェネリックリストを設定します。

CellStyle = new Style() 
{ 
    TargetType = typeof(DataGridCell), 
    Setters = 
    { 
     new Setter(DataGridCell.ForegroundProperty, new Binding("ExaminedAttributeValueColor")) 
    } 
} 

をしかし、それはちょうどElementStyleのように、動作しません:私はまた、このようCellStyleの代わりElementStyleをバインドしようとしました。私は多くのトラブルシューティングを見つけましたが、それらはすべてXAMLコーディングに関するものです。私はC#コードから何をしたいのですか?私はどうすればいいのですか? CellStyleまたはElementStyleのプロパティ以外のものを使用する必要がありますか?私はDataGridTextColumnFontStyleプロパティを見ましたが、私が知っているように、それはちょうどフォントファミリを定義しています。ここで

+0

MVAMパターンを使用しているので、それがXAMLコーディングで見つかったすべての例があります。 – Monty

+0

私がC#コードでdataGridを定義する理由は、実行時に列を定義する必要があります。私はWPFで新しく、私にとってそれを行うのが最も簡単な方法でした。とにかく私の場合には解決策があるはずですね。 –

+0

何でも可能ですが、それは正しいことを意味しません.... WPF \ XAMLでは、最も簡単な方法は通常、最良の方法ではありません。http://www.wpftutorial.net/datagrid.htmlを見てください。私はあなたのC#コードで、バインディング、スタイル、セッターとUIコントロールへの参照を見ることができます。それはXAMLでもっと簡単に行えます。 XAMLのマスタリングに力を注いだ場合、XAMLはコードビハインドよりもはるかに簡単です。正直なところ、私は5年前に同じ問題に直面した.... – Monty

答えて

0

はMVVMパターン以下

、フォアグラウンドを設定し、DataGrid内のセルの背景を加えたアイテムのドロップダウンコレクションのためにコンボボックスを追加するのは非常に迅速かつ基本的な例である

まず、BasePropertyChangedクラスを作成しますINotifyPropertyChangedのは実装して、我々はプロパティ値への変更が通知する。ここUI

public class BasePropertyChanged : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

は、あなたの「ExamineExamplesDataGridItem」クラスで、私は私が何をあなたの知らないcosをちょうどここで何かをしなければならなかった好きな場所、それが継承されます.....のように見える

ここでは0
public class ExamineExamplesDataGridItem : BasePropertyChanged 
{ 


    public ExamineExamplesDataGridItem() 
    { 
     PredictiveAttributeValues = new List<string>(); 
     PredictiveAttributeValues.Add("Predict 1"); 
     PredictiveAttributeValues.Add("Predict 2"); 
     PredictiveAttributeValues.Add("Predict 3"); 
     PredictiveAttributeValues.Add("Predict 4"); 
     PredictiveAttributeValues.Add("Predict 5"); 
    } 

    private List<string> _PredictiveAttributeValues; 

    public List<string> PredictiveAttributeValues 
    { 
     get { return _PredictiveAttributeValues; } 
     set { _PredictiveAttributeValues = value; NotifyPropertyChanged("PredictiveAttributeValues"); } 
    } 

    private string _Coll1; 

    public string Coll1 
    { 
     get { return _Coll1; } 
     set { _Coll1 = value; NotifyPropertyChanged("Coll1"); } 
    } 

    private string _Coll2; 

    public string Coll2 
    { 
     get { return _Coll2; } 
     set { _Coll2 = value; NotifyPropertyChanged("Coll2"); } 
    } 
} 

}

ViewModelには、あなたのビュー(XAML)がに結合すると、データの変更(ここではViewModelにで)として、そのタスクは、単にアプリケーション内のデータの状態を維持することであるということですユーザからの入力の結果、または外部ソースからの更新(すなわち、データベース)[表示が更新される(バインドとのDataContextを使用して)

class Base_ViewModel : BasePropertyChanged 
{ 

    private List<ExamineExamplesDataGridItem> _items; 

    public List<ExamineExamplesDataGridItem> items 
    { 
     get { return _items; } 
     set { _items = value; NotifyPropertyChanged("items"); } 
    } 

    public Base_ViewModel() 
    { 
     items = new List<ExamineExamplesDataGridItem>(); 
     items.Add(new ExamineExamplesDataGridItem() { Coll1 = "Row 1 Col 1", Coll2 = "Row 1 Col 2" }); 
     items.Add(new ExamineExamplesDataGridItem() { Coll1 = "Row 2 Col 1", Coll2 = "Row 2 Col 2" }); 
     items.Add(new ExamineExamplesDataGridItem() { Coll1 = "Row 3 Col 1", Coll2 = "Row 3 Col 2" }); 
    } 
} 

ここでXAMLは、どのようにすべてのスタイルに気づく、セッターとバインディングはXAMLで行われ...

<Grid> 

    <DataGrid Name="DataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding items}" > 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Combobox"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox SelectedIndex="0" ItemsSource="{Binding PredictiveAttributeValues}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTextColumn Binding="{Binding Coll1}" Header="Column 1"> 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell"> 
         <Setter Property="Background" Value="Blue" /> 
         <Setter Property="Foreground" Value="White" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Binding="{Binding Coll2}" Header="Column 2"> 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell"> 
         <Setter Property="Background" Value="Red" /> 
         <Setter Property="Foreground" Value="White" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 
      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

</Grid> 

と最後にデータコンテキストが.....今、私は単にこの例を簡素化するのではなく、通常のコードも、この1行は、XAMLで行くとコードビハインドでこれを設定しているが、あなたはアプリケーション名を使用してXAMLでそのビューモデルへの参照を追加する必要があるため、それは、より複雑なXAMLコードが必要になると私は[あなた]アプリケーションを呼び出したり、フォルダ構造

// in the Code - Behind file of you WPF Window 
InitializeComponent(); 
// Set Data Context for the View 
this.DataContext = new Base_ViewModel(); 

それが何であるかを知りません私が知っている多くのコードのように見えますが、あなたがそれを分解すると、そこにはあまり存在しません。あなたは今、XAMLのUIコントロールをスタイル設定して変更することは自由です。最終テストはこれです。純粋なMVVMアプリケーションでは、すべてのUIコントロールをコメントアウトする必要があります( XAML)、コードはエラーなしでコンパイルされます.....

+0

それは私が本当に必要なものではありません。しかし、私は、このようなものを使って、私が必要とすることをすることができると思います。とにかく私は結果について書きます。ご回答有難うございます! –