This is my first experience with ToggleSwitch. What I am trying to achieve is to show different data from the list using different ToggleSwitches.
I have a ListView with multiple TextBlocks and one ToggleSwitch for each row of data.
Then I populate ListView with data from List. (List is populated using class that forsees
public ToggleSwitch Switch {get; set;}
Here is how I try to get ToggleSwitch data from each row:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
for (int a = 0; a < jointList.Count; a++)
{
jointList[a].Switch = sender as ToggleSwitch;
if (jointList[a].Switch != null)
{
if (jointList[a].Switch.IsOn == true)
{
ToggleTest.Text = jointList[a].ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
}
Unfortunately I am getting the same(last added) productId from all of the ToggleSwitches as if they were pointing to same place.
EDIT> I have rewritten the code as touseef suggested:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
for (int i = 0; i < jointList.Count; i++)
{
if (jointList[i].Value == true)
{
ToggleTest.Text = jointList[i].ProductId.ToString();
// ToggleTest.Text = jointList[a].ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
But now nothing shows up.
EDIT: Here is another attempt to resolve the problem:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
foreach (var record in jointList)
{
if (record.Value == true)
{
ToggleTest.Text = record.ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
And now only one ToggleSwitch works, the one that corresponds to the last added record (I was pulling ProductId of the jointList). None of the other ToggleSwitches work. They don't return any data when using the code above.