<DataGrid ItemsSource="{Binding FilesToAdd}" Height="100" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="100" AutoGenerateColumns="False" Visibility="{Binding FilesAreQueued, Converter={StaticResource BoolToVisConverter}}">
<DataGrid.Resources>
<app:BindingProxy x:Key="Proxy" Data="{Binding}"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="File Will be Uploaded As" Binding="{Binding FileDisplayText}"/>
<DataGridTextColumn Header="Size" Binding="{Binding FileSizeInText}"/>
<DataGridComboBoxColumn ItemsSource="{Binding AvailableDocumentTypes}" Header="Document Type" Visibility="{Binding Data.DocumentTypesAreRequired, Converter={StaticResource BoolToVisConverter}, Source={StaticResource Proxy} }">
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
Per Kevin的建议,我试过了:
<DataGridComboBoxColumn ItemsSource="{Binding DataContext.AvailableDocumentTypes, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window }}}" Header="Document Type" Visibility="{Binding Data.DocumentTypesAreRequired, Converter={StaticResource BoolToVisConverter}, Source={StaticResource Proxy} }"/>
也尝试过:
<DataGridComboBoxColumn ItemsSource="{Binding Path=DataContext.AvailableDocumentTypes, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window }}}" Header="Document Type" Visibility="{Binding Data.DocumentTypesAreRequired, Converter={StaticResource BoolToVisConverter}, Source={StaticResource Proxy} }"/>
但是我收到了无法找到的错误。
仅供参考,我的DataContext按以下方式设置:
在XAML中:
<Window x:Class="VEUploader.WPF.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:VEUploader.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
Title="{Binding WindowTitle}" SizeToContent="WidthAndHeight"
DataContext="{StaticResource UploaderViewModel}"
AllowDrop="True"
>
在代码中(所以我可以执行依赖注入):
public MainWindow()
{
var uploaderViewModel = new UploaderViewModel(ObjectFactory.GetInstance<IVEDocumentService>(), ObjectFactory.GetInstance<IArgumentSettingsRetriever>(), ObjectFactory.GetInstance<IVEBudgetService>());
Resources["UploaderViewModel"] = uploaderViewModel;
InitializeComponent();
}
答案 0 :(得分:4)
我面前没有编译器,但我相信语法看起来像这样:
{Binding DataContext.MyProperty, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type MyParentControl}}}
编辑:实际上我自己做了一些测试,由于某些原因,DataGridComboBoxColumn
无法找到项目源,但如果你模板化你自己的组合框列,它就可以了。不知道为什么会这样,但这可能是一个可以接受的工作。
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding FilesToAdd}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Document Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource=
"{Binding DataContext.AvailableDocumentTypes,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window }}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>