簡體   English   中英

是否可以從列表視圖項模板中訪問控件視圖模型中的屬性?

[英]Is it possible to access a property from the control's view model from a listview item template?

我正在創建一個功能,當您瀏覽菜單項時,您將獲得面包屑返回。 為了在我的Windows 8應用程序中創建它,我正在生成一個項目集合,並在我瀏覽菜單時添加到該集合。

用於顯示面包屑的xaml代碼是:

<ListView VerticalAlignment="Top"
          HorizontalAlignment="Left"
          Margin="120,60,0,0"
          ItemsSource="{Binding Parents}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text=">>" />
                <Button Content="{Binding Name}" Command="{Binding OpenCommand}" CommandParameter="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

雖然這有效,但我對按鈕的綁定並不是百分之百滿意。 問題是我正在調用已在我的整體控件的視圖模型上啟動並運行的功能,這似乎要求我在我的內部項目命令上有一個OpenCommand屬性。

是否可以將我的按鈕的Command=屬性綁定到控件的整體視圖模型上的命令,而不是列表項本身?

這可能是這樣的:

<ListView x:Name="listView" ...>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text=">>" />
                <Button Content="{Binding Name}"
                    Command="{Binding DataContext.OpenCommand, ElementName=listView}"
                    CommandParameter="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
    ...
</ListView>

暫無
暫無

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

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