0
在下面的代碼片段中,我有一個綁定在viewmodel上搜索「Checked」。但它不在我的ViewModel中,它在我的DataGrid ItemsSource中的項目上。DatagridTemplateColumn複選框綁定在視圖模型上搜索屬性
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Checked, UpdateSourceTrigger=PropertyChanged}" />
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如何使綁定使用ItemsSource中的項目? 我已經搜查高和低,但不能發現問題...
編輯:DataGrid的結合:
<DataGrid MaxHeight="250" VerticalScrollBarVisibility="Auto" ItemsSource="{Binding Questions}" CanUserAddRows="False" AutoGenerateColumns="False" Width="Auto">
<DataGrid.Columns>
<DataGridTemplateColumn ..... etc.. above snippet here
...
</DataGridTemplateColumn.....
</DataGrid.Columns>
</DataGrid>
視圖模型:
public class Question
{
public bool Checked { get; set; }
public string Text { get; set; }
/// <summary>
/// Answers to show if the Question is checked
/// </summary>
public List<Answer> Answers { get; set; }
}
public class MyViewModel : ViewModelBase
{
private readonly IEnumerable<Question> _Questions;
/// <summary>
/// Questions for Definition of Done
/// </summary>
public IEnumerable<Question> Questions
{
get
{
return _Questions;
}
}
}
顯示代碼視圖模型和Itemsouce結合。 –
您需要在Question類上實現INotifyPropertyChanged,並在DataTemplate中給DataType =「{x:Type local:Question}」。 –