我想这是一个简单的数据绑定问题,但我无法让它工作。我有一个 DataGrid、一个带有 Observable 集合的 ViewModel 和一个表示该集合中对象的类。DataGrid 有两列:DataGridTextColumn 和DataGridTemplateColumn。数据与 DataGridTextColumn 的绑定有效,但与 DataGridTemplateColumn 的绑定无效。相应的列只是保持为空。
这是代码:
这是我的数据网格:
<DataGrid x:Name="tdg_Memory" Grid.Column="1" Margin="0" Grid.Row="4" VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="MemoryIndexColumn" Header="Index"/>
<DataGridTemplateColumn x:Name="MemorySolutionColumn" CellTemplate="{DynamicResource MemorySolutionCellTemplate}" Header="Solution"/>
<DataGrid.Columns>
<DataGrid>
这是数据模板:
<DataTemplate x:Key="MemorySolutionCellTemplate">
<Grid x:Name="grd_RootGrid" Height="Auto" Width="Auto" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label x:Name="lbl_ValueCaption" Content="Value:" Margin="0" Grid.Row="0" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<Label x:Name="lbl_FitnessCaption" Content="Fitness:" Margin="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<Label x:Name="lbl_ValueValue" Content="65665" Grid.Column="1" Height="Auto" Margin="12,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<Label x:Name="lbl_FitnessValue" Content="121212" Grid.Column="1" Margin="12,0,0,0" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
数据绑定发生在后面的代码中:
// setting the itemsSource (seems to work)
jobItem.tdg_Memory.ItemsSource = jobModel.Memory;
// binding of the first column (DataGridTextColumn) seems to work
Binding memoryIndexColumnBinding = new Binding();
memoryIndexColumnBinding.Path = new PropertyPath("Index");
jobItem.MemoryIndexColumn.Binding = memoryIndexColumnBinding;
这是 ViewModel:
// the observable collection that holds the data for the DataGrid
private ThreadSafeObservableCollection<MemoryItemRepresentative> _memory;
public ThreadSafeObservableCollection<MemoryItemRepresentative> Memory {
get { return _memory; }
set {
_memory = value;
FirePropertyChanged("Memory");
}
}
以下是集合中包含的对象:
public class MemoryItemRepresentative : ViewModelBase {
private int _index;
public int Index {
get { return _index; }
set {
_index = value;
FirePropertyChanged("Index");
}
}
private MPBILSolution _solution;
public MPBILSolution Solution {
get { return _solution; }
set {
_solution = value;
FirePropertyChanged("Solution");
}
}
public MPBILPropabilityVector PropabilityVector;
public MemoryItemRepresentative () {
}
}
MBILSolution 是(但这应该与该问题无关):
public class MPBILSolution : ISolution {
public int Position { get; set; }
public BinaryNumber Value { get; set; }
public double Fitness { get; set; }
}
我现在要做的是将 DataGrid 第一列(DataGridTextColumn)绑定到 MemoryItemRepresentative 类的 Index 属性,该属性可以正常工作。
此外,我想显示 MPBILSolution 对象的内容(由属性位置、值和适应度组成)绑定到第二列(DataGridTemplateColumn)。出于这个原因,我构建了包含 Value 属性标签和 Fitness 属性标签的 DataTemplate。
我没有发布不起作用的实际绑定代码,因为我的问题似乎是 DataGrid 的相应列始终为空。至少 DataTemplate 中的静态标签(用于字幕)应该是可见的?
所以我非常感谢任何帮助。任何进一步的代码请求...
附录:
好吧,现在实际的绑定似乎出现了问题:在 DataGrid 中,我现在可以看到列内的 DataTemplate 但 MPBILSolution 对象的值丢失了:
这是我如何将此对象绑定到 DataTemplate:
DataTemplate memorySolutionCellTemplate = (DataTemplate)jobItem.FindResource("MemorySolutionCellTemplate");
DependencyObject o = memorySolutionCellTemplate.LoadContent();
Label l = (Label)VisualTreeAssistant.FindFrameworkElement(o, "lbl_FitnessValue");
Binding fitnessValueBinding = new Binding();
fitnessValueBinding.Path = new PropertyPath("Fitness");
fitnessValueBinding.Mode = BindingMode.OneWay;
l.SetBinding(Label.ContentProperty, fitnessValueBinding);
对象 l 似乎得到了正确的 Label 对象,所以问题似乎出在这个绑定块的最后四行?
请再次帮助:)