1

Self-taught programmer, would love any constructive criticism regarding my code.

I have a ListView that will have ListViewItems that I want to customize.

The ListViewItem I have made has two TextBlocks and a ToggleSwitch. When the ToggleSwitch is switched On/Off I want it to call a method from an instantiate object, or call a method from the same form, but somehow retrieve the object that initially loaded into the DataTemplate.

Here is the XAML so far:

    <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>

Right now it is calling listViewVideoFolder_toggled

Before I was trying to use Toggled="{Binding StartCrawling()}"

Here is the AddVideoFolderModel object that I am binding the listviewitems to

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() + ")";
        //}
    }
     }

What must I implement to accomplish this?

Illuminati
  • 538
  • 7
  • 22

1 Answers1

1

At first, you can add property to your model and bind to IsOn property in ToggleSwitch using TwoWay mode binding. Is this case, your model must implement INotifyPropertyChanged

    private bool _isNeedCrawle;

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

                NotifyPropretyChanged("IsNeedCrawle");
            }
        }
    }

At second, you can use XAML Behavior SDK. In this case, you must Add reference to library (look how to do it), and change method modifier from private to public

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>
Community
  • 1
  • 1
Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41