2016-02-14 81 views
1

自學程序員,會喜歡任何有關我的代碼的建設性批評。UWP從DataTemplate對象調用方法

我有一個ListView,將ListViewItems,我想要自定義。

我所做的ListViewItem有兩個TextBlocks和一個ToggleSwitch。當ToggleSwitch打開/關閉時,我希望它從一個實例化對象中調用一個方法,或者調用一個來自同一個表單的方法,但以某種方式檢索最初加載到DataTemplate中的對象。

這裏是XAML至今:

<ListView x:Name="listViewAddedVideoFolders" Grid.Row="1" DoubleTapped="listViewAddedVideoFolders_DoubleTapped" SelectionChanged="listViewAddedVideoFolders_SelectionChanged" HorizontalContentAlignment="Stretch"> 
     <ListView.ItemTemplate> 
      <DataTemplate>      
       <Grid HorizontalAlignment="Stretch"> 
        <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Directory}"/>       
        <Grid HorizontalAlignment="Right"> 
         <StackPanel> 
          <TextBlock Text="Find Videos: "></TextBlock> 
          <ToggleSwitch Toggled="listViewVideoFolder_toggled" /> 
         </StackPanel> 
        </Grid> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 

現在它被調用listViewVideoFolder_toggled

之前,我試圖用切換= 「{結合StartCrawling()}」

這裏AddVideoFolderModel對象,我綁定listviewitems到

namespace Movie_Management_Windows_10.Models 
{ 
    public class AddVideoFolderModel 
    { 
     public static ObservableCollection<AddVideoFolderModel> MyVideoFolderModels = new ObservableCollection<AddVideoFolderModel>(); 
     public int VideosFound { get; set; } 
     public string Directory { get; set; } 
     public string DirectoryName { get; set; } 
     private bool isCrawling = false; 
     public bool HasBeenCrawled = false; 

     private void startCrawling() 
     { 
      AppShell.Current.NotifyUser("Crawling began", AppShell.NotifyType.StatusMessage); 
     } 


     //public override string ToString() 
     //{ 
     // return Directory + " (" + VideosFound.ToString() + ")"; 
     //} 
    } 
    } 

我必須執行哪些操作才能完成此操作?

回答

1

首先,您可以將屬性添加到您的模型並使用TwoWay模式綁定綁定到IsOn屬性ToggleSwitch。是這種情況下,你的模型必須實現INotifyPropertyChanged

private bool _isNeedCrawle; 

    public bool IsNeedCrawle 
    { 
     get 
     { 
      return _isNeedCrawle; 
     } 
     set 
     { 
      if (_isNeedCrawle != value) 
      { 
       _isNeedCrawle = value; 
       if (_isNeedCrawle) 
       { 
        startCrawling(); 
       } 

       NotifyPropretyChanged("IsNeedCrawle"); 
      } 
     } 
    } 

在第二,你可以使用XAML行爲SDK。在這種情況下,您必須對庫(look how to do it),及更換方法改性劑添加引用從privatepublic

xmlns:i="using:Microsoft.Xaml.Interactivity" 
xmlns:core="using:Microsoft.Xaml.Interactions.Core" 

<ToggleSwitch> 
    <i:Interaction.Behaviors> 
     <core:EventTriggerBehavior EventName="Toggled"> 
      <core:CallMethodAction MethodName="StartCrawling" TargetObject="{Binding }"/> 
     </core:EventTriggerBehavior> 
    </i:Interaction.Behaviors> 
</ToggleSwitch> 
+0

我也嘗試INotifyPropertyChanged的,我會再追求這一點。謝謝! – Illuminati

+0

名稱空間不存在與那些對象 – Illuminati

+0

哪一個?第一種情況或第二種? –