0

演習と目的の 2 つのオブジェクトがあり、各演習には目的が 1 つあります。コード ビハインドでは、Exercises DataGrid の ItemsSource を設定します。

 dgExercicios.ItemsSource = Repositorio<Exercicio>.GetAll();

そして私のXAMLで。

    <DataGrid Name="dgExercicios" CellStyle="{StaticResource CellStyle}" CanUserSortColumns="True" CanUserResizeColumns="True" AutoGenerateColumns="False">
         <DataGrid.Columns>
             <!--This bind works (Exercice.Nome)-->
             <DataGridTextColumn Header="Nome" Binding="{Binding Nome}" Width="200"   />
             <!--This bind DONT works (I Trying to bind to Exercise.Objective.Descricao)-->                     
             <DataGridTemplateColumn Header="Objetivo"  Width="80" >
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate  >
                             <StackPanel>
                                 <TextBlock Text="{Binding Path=Objective.Descricao}" T />
                              </StackPanel>
                          </DataTemplate>
                     </DataGridTemplateColumn.CellTemplate>
                 </DataGridTemplateColumn>
          </DataGrid.Columns>
      </DataGrid>

私がやりたいことは、プロパティ Exercise.Objective.Descricao を Exercice.Nome の TextBlock にバインドすることです

別の質問ですが、この場合 DataGridTemplateColumn が必要ですか?

4

1 に答える 1

1

エクササイズクラスにObjectiveというプロパティがあり、ObjectiveクラスにDescricaoというプロパティがある場合は機能します。

public class Exercicio
{
    public string Nome { get; set; }
    public Objective Objective { get; set; }

    public Exercicio(string nome, Objective objective)
    {
        this.Nome = nome;
        this.Objective = objective;
    }
}

public class Objective
{
    public string Descricao { get; set; }

    public Objective(string d)
    {
        this.Descricao = d;
    }
}

public MainWindow()
{
    InitializeComponent();

    var items = new ObservableCollection<Exercicio>(new[] {
        new Exercicio("Exercicio1", new Objective("Objective1")),
        new Exercicio("Exercicio2", new Objective("Objective2")),
        new Exercicio("Exercicio3", new Objective("Objective3"))
    });

    dgExercicios.ItemsSource = items;
}

また、文字列を表示するだけの場合は、DataGridTemplateColumnは必要ありません。

<!-- Works now (also in a normal text column) -->
<DataGridTextColumn Binding="{Binding Path=Objective.Descricao}" Header="Objetivo" Width="80" />
于 2012-10-27T20:22:39.607 に答える