c# - How do I use XAML to bind a DatagridComboBoxColumn to a ViewModel property that isn't in the DataGrid's ItemsSource? -
what i'm attempting
- i have datagrid that's bound list called filestoadd, part of viewmodel.
- i have combobox column on datagrid.
- i bind combobox's selected item documenttype property of filestoadd list item.
- but, have list of available choices come availabledocumenttypes, property of viewmodel itself, not of filestoadd list.
the xaml
<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 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> update 1: xaml i'm trying + datacontext clarification
per kevin's suggestion, tried:
<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} }"/> also tried:
<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} }"/> but receive error can't found.
fyi, datacontext set in following way:
in 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" > in code (so can dependency injection):
public mainwindow() { var uploaderviewmodel = new uploaderviewmodel(objectfactory.getinstance<ivedocumentservice>(), objectfactory.getinstance<iargumentsettingsretriever>(), objectfactory.getinstance<ivebudgetservice>()); resources["uploaderviewmodel"] = uploaderviewmodel; initializecomponent(); }
i don't have compiler in front of me, believe syntax this:
{binding datacontext.myproperty, relativesource={relativesource mode=findancestor, ancestortype={x:type myparentcontrol}}} edit: doing testing of own, reason datagridcomboboxcolumn somehow cannot find item source, if template own combo box column, works fine. not sure why is, may acceptable work around.
<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>
Comments
Post a Comment