简体   繁体   中英

How to get access to ToggleSwitch instance within ListView in C# UWP?

I opened the question here but we cannot come to the solution for my problem. I decided to create new question as we came to some assumptions and the former question does not refer to the real problem(we thought it is the problem with binding but as you will read on it is not).

In few words I have a ListView with data from list called jointList. The list is doing well and it has all the data necessary. (I checked it)

On each row of the ListView I put a ToggleSwitch(in xaml) and then I try to do something with each of the switches.

Each switch should correspond to the data from the same row.

I created Toggled event that should apply to all toggleSwitches like this:

private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
    {

        foreach (var product in jointList)
        {

            if (product.IsOn == true)
            {
                ToggleTest.Text = product.ProductId.ToString(); // this is for testing only, later I would do something with the data retrieved
                ToggleTest.Visibility = Visibility.Visible;
            }
            else
            {
                ToggleTest.Visibility = Visibility.Collapsed;
            }

        }

    }

But this is making only one toggleSwitch work. It's the switch that corresponds to the last added product to the list ( I am guessing that it is refering to the last Id). The other switches return nothing as if the method was not iterating through the list correctly or as if there was only one switch hooked up.

So, is it possible to get all switches up and running by using just one Toggled event as I attempt to do?

Here's a sample which shows one way.

In this example we have the following Product view model:

public class Product : INotifyPropertyChanged
{
    private string _name;

    public string Name
    {
        get => _name;
        set
        {
            if (value == _name) return;
            _name = value;
            OnPropertyChanged();
        }
    }

So just a single Name-property.

Then we have MainPage where we create a collection of products:

    private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
    {
        var items = new ObservableCollection<Product>();
        for (int i = 0; i < 9; i++)
        {
            items.Add(new Product($"item {i}"));
        }

        this.Items.ItemsSource = items;
    }

And the XAML which creates the view:

    <ListView Loaded="FrameworkElement_OnLoaded" x:Name="Items">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <TextBlock x:Name="RowContent" Text="{Binding Name}"/>
                    <ToggleSwitch x:Name="Toggle" Grid.Column="1" Toggled="Toggle_OnToggled"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

The result:

起始情况

Now we want to change the text when user toggles the switch. This is done in Toggle_OnToggled-event handler:

    private void Toggle_OnToggled(object sender, RoutedEventArgs e)
    {
        var toggle = (ToggleSwitch) sender;

        var dataContext = ((Grid)toggle.Parent).DataContext;
        var dataItem = (Product) dataContext;

        dataItem.Name = $"Toggled {toggle.IsOn}";
    }

So after a few toggles:

切换

Mikael Koskinen has delivered the answer to my problem.

Most of my code was correct and identical to his solution, apart from the last bit that is OnToggled event handler. Here is the working andd correct handler:

private void Toggle_OnToggled(object sender, RoutedEventArgs e)
    {
        var toggle = (ToggleSwitch)sender;

        var dataContext = ((Grid)toggle.Parent).DataContext;
        var dataItem = (ScheduleList)dataContext;

        ToggleTest.Text = dataItem.ProductId;
    }

My previous version of handler didn't include the important bit, that is dataContext and dataItem.

It works like a charm now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM