无法将集合属性绑定到数据网格行模板
<DataTemplate x:Key="RowDetailsTemplate" DataType="{x:Type summons:IndividualDefendantSearchViewModel}">
<controls:ExtendedDataGrid
x:Name="RowDetails"
CanUserSortColumns="True"
ContentWidth="910"
DataContext="{Binding ElementName=Parent, Path=DataContext.GroupedSearchResults}"
Focusable="False"
HeadersVisibility="None"
IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding Path=SelectableIndividualDefendantSearchResults}"
ShowRowSelection="False">
<controls:ExtendedDataGrid.Columns>
<DataGridTextColumn
Width="*"
Binding="{Binding Name}"
Header="{localization:LanguageBinding ResourceKey=Name}" />
</controls:ExtendedDataGrid.Columns>
</controls:ExtendedDataGrid>
</DataTemplate>
我的数据模板
public class SelectableIndividualDefendantSearchResult : CheckedListItem<SearchResult>
{
public string Name
=> $"{Item.FirstName} {(string.IsNullOrEmpty(Item.Insertion) ? "" : Item.Insertion + " ")}{Item.LastName}";
}
public class GroupedSelectableIndividualDefendantSearchResult: EnhancedBindableBase
{
public DateTime? SearchDate { get; set; }
public ObservableCollection<SelectableIndividualDefendantSearchResult>
_selectableIndividualDefendantSearchResults;
public ObservableCollection<SelectableIndividualDefendantSearchResult>
SelectableIndividualDefendantSearchResults {
get { return _selectableIndividualDefendantSearchResults; }
set { SetProperty(ref _selectableIndividualDefendantSearchResults, value); }
}
}
我的财产:
public ObservableCollection<GroupedSelectableIndividualDefendantSearchResult> GroupedSearchResults
{
get { return _groupedSearchResults; }
set { SetProperty(ref _groupedSearchResults, value); }
}
主DataGrid ItemSource是
ItemsSource="{Binding GroupedSearchResults}"
我可以看到SearchDate绑定到主行,但是RowDataTemplate是空字符串......我做错了什么?
答案 0 :(得分:0)
不要将DataContext
绑定到{Binding ElementName=Parent, Path=DataContext.GroupedSearchResults}
:
<DataTemplate x:Key="RowDetailsTemplate">
<controls:ExtendedDataGrid
x:Name="RowDetails"
CanUserSortColumns="True"
ContentWidth="910"
Focusable="False"
HeadersVisibility="None"
IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding Path=SelectableIndividualDefendantSearchResults}"
ShowRowSelection="False">
<controls:ExtendedDataGrid.Columns>
<DataGridTextColumn
Width="*"
Binding="{Binding Name}"
Header="{localization:LanguageBinding ResourceKey=Name}" />
</controls:ExtendedDataGrid.Columns>
</controls:ExtendedDataGrid>
</DataTemplate>
答案 1 :(得分:0)
DataTemplate的DataContext类型为IndividualDefendantSearchViewModel
。所以你的ItemsSource绑定指向IndividualDefendantSearchViewModel.SelectableIndividualDefendantSearchResults
,对吧?
您必须绑定到SelectableIndividualDefendantSearchResults
属性所在的另一个DataContext。
您可以使用以下方法执行此操作:
{Binding DataContext.SelectableIndividualDefendantSearchResults, RelativeSource={RelativeSource AncestorType=UserControl}}
UserControl保存带有target属性的DataContext,您需要根据自己的需要调整它(如果它与UserControl不同)。
增加:
mm8是对的。不要绑定到DataGrid的DataContext,没有必要。