2016-05-23 48 views
1

我的Windows通用頁面上有一個ListView。我用一個用戶控件來定義我的ItemTemplate,這樣我可以使用RelativePanel和VisualStateManager來控制我的項目出現如何根據屏幕尺寸...DataBinding ListView ItemTemplate UserControl

<ListView ItemsSource="{Binding Path=AllThings}" 
      ItemContainerStyle="{StaticResource ListViewItemStyle}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <local:CrossingControl /> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

我有我的用戶一個按鈕,我想它的命令綁定到是列表本身的DataContext的視圖模型的命令屬性...

<UserControl ...> 
    <RelativePanel> 
    <StackPanel x:Name="crossedEntryPanel"> 
     <Button Command="{Binding DataContext.DeleteCommand, 
          RelativeSource={RelativeSource Mode=TemplatedParent}}" 
            CommandParameter="{Binding}" 

我已經使用的ElementName約束力的嘗試,但它似乎沒有工作(我想是因爲我的ListView元素名稱在另一個xaml文件中定義)。我也嘗試了上面的RelativeSource綁定,但似乎也沒有工作。我怎樣才能正確地綁定這個?

+0

這是怎麼回事:http://stackoverflow.com/questions/19522202/how-to-call-功能在一個主視圖模型從其他視圖模型 – Alamakanambra

+0

不完全解決我的問題,我害怕。在祖先類型似乎不可用在Windows通用 –

+0

如何在'CrossingControl'內移動'DeleteCommand'? –

回答

2

您可以使用Tag財產的保存的ListViewDataContext,並用它在UserControl

這是如何做

<ListView ItemsSource="{Binding Path=AllThings}" x:Name="listview" 
         ItemContainerStyle="{StaticResource ListViewItemStyle}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <local:CrossingControl Tag="{Binding DataContext,ElementName=listview}" /> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

<UserControl x:Name="usercontrol" ...> 
    <RelativePanel> 
    <StackPanel x:Name="crossedEntryPanel"> 
     <Button Command="{Binding Tag.DeleteCommand,ElementName=usercontrol}" 
            CommandParameter="{Binding}" 

的RelativeSource = {的RelativeSource模式= TemplatedParent}}「: -這指向controltemplatebutton

相關問題