簡體   English   中英

xaml如何綁定到其他來源?

[英]xaml How to bind to a different source?

我在C#代碼中設置Items ,如下所示:

this.DefaultViewModel["Items"] = items;

並且我已經在xaml頁面中定義了它,如下所示:

DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

現在我可以在GridView使用Items

<GridView ItemsSource="{Binding Items}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid Width="{Binding customWidth}">
                <!-- The rest-->
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

但是問題是我希望內部網格使用其他來源:

this.DefaultViewModel["customWidth"] = 300;

如何使內部網格使用與其父級GridView不同的源? 謝謝。

您可以為頁面命名(例如x:Name =“ myPage”),然后

<GridView ItemsSource="{Binding Items}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid Width="{Binding DefaultViewModel.customWidth, ElementName=myPage}">
                <!-- The rest-->
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

也就是說,可以使用ElementName退出當前的綁定范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM