DataBrid与DataGrid和DataTemplate的问题

时间:2012-07-15 18:53:08

标签: c# wpf xaml data-binding datagrid

我想这是一个简单的数据绑定问题,但我不能让它工作。我有一个DataGrid,一个带有Observable Collection的ViewModel和一个表示此集合中对象的类。 DataGrid有两列:DataGridTextColumn和DataGridTemplateColumn。 数据与DataGridTextColumn的绑定有效,但与DataGridTemplateColumn的绑定不起作用。相应的列只是保持为空。

以下是代码:

这是我的DataGrid:

<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:

<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对象的内容(由Properties Position,Value和Fitness组成)绑定到第二列(DataGridTemplateColumn)。出于这个原因,我构建了DataTemplate,它包含Value属性的标签和Fitness属性的标签。

我还没有发布不起作用的实际绑定代码,因为我的问题似乎是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对象,所以问题似乎在于这个绑定块的最后四行?

请再次帮助:)

1 个答案:

答案 0 :(得分:1)

为什么DynamicResource?如果您可以使用StaticResource这样做,DynamicResource可以对我认为的错误(可能检查“输出”窗口)保持安静...