简体   繁体   中英

WPF Relative binding exception

Given a usercontrol bound to a viewmodel defined as follows

class MyViewModel
{
    public DataView MyView { get; set; }
    public DataView MyTypes { get; set; }
}

and the XAML is roughly marked as follows

<Grid>
    <dxg:GridControl AutoPopulateColumns="True" Name="gridControl1" ItemsSource="{ Binding MyView}">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="col1">
                <dxg:GridColumn.EditTemplate>
                    <ControlTemplate>
                        <dxe:ComboBoxEdit Name="cmbTypes" 
                       ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                            Path=ViewModel.MyTypes}"/>
                    </ControlTemplate>
                </dxg:GridColumn.EditTemplate>
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="col2"/>
            <dxg:GridColumn FieldName="col3" Width="75"/>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView Name="tableView1" AutoWidth="True" ShowTotalSummary="True" />
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

bound as follows

internal MyViewModel ViewModel
{
    get { return (MyViewModel)DataContext; }
    set { DataContext = value; }
}

Of course the ComboBox does not show the selections from ListTypes which does have values

Question is - how to reference the ListTypes property of the ViewModel from Grid.Column where the Grid is Bound to a DataView MyItems which does not have the property ListTypes, which is at the same level as ListTypes !!!

Any help is appreciated

If I've understood the question correctly, I'd expect the following to work:

{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
                        Path=DataContext.MyTypes}

It may be that Path=ViewModel.MyTypes is not working simply because you haven't implemented change notification on the ViewModel property. As I say, though - you should be able to get along fine with Path=DataContext.MyTypes .

Note: you seem to refer to the property as both MyTypes and ListTypes in the question - not sure which is correct for your object model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM