Listview 中的 UWP C# Togglesswitch - 单击 Togglesswitch 时如何获取行的项目
UWP C# Toggleswitch inside Listview - How to get items of Row when clicked on Toggleswitch
所以我在 Toggleswitch 上有这个 Listview。我想要做的是触发像 ItemClick="Event_Overview_Teams_ItemClick"
这样的事件
但与
Toggled="TeamPresent_Toggled"
并处理点击行的 TeamID 等项目。
<ListView x:Name="Event_Overview_Teams" HorizontalAlignment="Center" Width="1787" Margin="0,48,0,10" IsItemClickEnabled="True" ItemClick="Event_Overview_Teams_ItemClick" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="TeamName" Grid.Column="0" Text="{Binding TeamName}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamID" Grid.Column="1" Text="{Binding TeamID}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamNumber" Grid.Column="2" Text="{Binding TeamNumber}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamClass" Grid.Column="3" Text="{Binding TeamClass}" TextWrapping="Wrap" />
<TextBlock x:Name="DeliverTime" Grid.Column="4" Text="{Binding DeliverTime}" TextWrapping="Wrap" />
<!--<TextBlock x:Name="TeamPresent" Grid.Column="5" Text="{Binding TeamPresent}" TextWrapping="Wrap" />-->
<ToggleSwitch x:Name="TeamPresent" Grid.Column="5" IsOn="{Binding TeamPresent}" Toggled="TeamPresent_Toggled"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
非常感谢您
当你触发TeamPresent_Toggled事件时,你可以通过获取DataContext[=15=获取当前item绑定的viewmodel ] 的拨动开关。然后从您获得的视图模型中更改 属性(例如 TeamID)。
private void TeamPresent_Toggled(object sender, RoutedEventArgs e)
{
var toggle = (ToggleSwitch)sender;
var dataContext = toggle.DataContext;
MyViewModel dataItem = (MyViewModel)dataContext;
dataItem.TeamID = "MyNewTeamId";
......
}
所以我在 Toggleswitch 上有这个 Listview。我想要做的是触发像 ItemClick="Event_Overview_Teams_ItemClick"
这样的事件
但与
Toggled="TeamPresent_Toggled"
并处理点击行的 TeamID 等项目。
<ListView x:Name="Event_Overview_Teams" HorizontalAlignment="Center" Width="1787" Margin="0,48,0,10" IsItemClickEnabled="True" ItemClick="Event_Overview_Teams_ItemClick" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="TeamName" Grid.Column="0" Text="{Binding TeamName}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamID" Grid.Column="1" Text="{Binding TeamID}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamNumber" Grid.Column="2" Text="{Binding TeamNumber}" TextWrapping="Wrap" />
<TextBlock x:Name="TeamClass" Grid.Column="3" Text="{Binding TeamClass}" TextWrapping="Wrap" />
<TextBlock x:Name="DeliverTime" Grid.Column="4" Text="{Binding DeliverTime}" TextWrapping="Wrap" />
<!--<TextBlock x:Name="TeamPresent" Grid.Column="5" Text="{Binding TeamPresent}" TextWrapping="Wrap" />-->
<ToggleSwitch x:Name="TeamPresent" Grid.Column="5" IsOn="{Binding TeamPresent}" Toggled="TeamPresent_Toggled"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
非常感谢您
当你触发TeamPresent_Toggled事件时,你可以通过获取DataContext[=15=获取当前item绑定的viewmodel ] 的拨动开关。然后从您获得的视图模型中更改 属性(例如 TeamID)。
private void TeamPresent_Toggled(object sender, RoutedEventArgs e)
{
var toggle = (ToggleSwitch)sender;
var dataContext = toggle.DataContext;
MyViewModel dataItem = (MyViewModel)dataContext;
dataItem.TeamID = "MyNewTeamId";
......
}