Search code examples
c#xamluwp

In DataTemplate ,how to get the page's field by databinding?


        <ListView
            x:Name="List"
            ItemsSource="{x:Bind taglist}"
            SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="x:String">
                    <RelativePanel HorizontalAlignment="Stretch">
                        <TextBlock
                            x:Name="str"
                            Text="{x:Bind}" />
                        <ComboBox
                            ItemsSource="{binding source=combolist}"/>
                    </RelativePanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

combolist is a collection of string ,field of page insatnce.

the textblock control in datatemplate use a list as datasource,but the comboxes use another datasource (only datasource is same,but not mean same selection and control action).

i tried to use {binding} markupextension, but this kind of databinding cannot find the varity.


Solution

  • Give the Page a name:

    <Page x:Name="thePage" ...
    

    Bind to it using an ElementName:

    <ComboBox ItemsSource="{Binding Path=combolist, ElementName=thePage}"/>
    

    Finally, you need to turn combolist into a public property as you cannot bind to fields:

    public List<string> combolist { get; set; }