在listview xamarin窗体中切换开关时访问名称
我有以下代码,其中包括一个列表视图。listview包含一个标签和一个自定义开关在listview xamarin窗体中切换开关时访问名称,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有以下代码,其中包括一个列表视图。listview包含一个标签和一个自定义开关 <ListView x:Name="myListView" Margin="20" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell IsEnabled="Fal
<ListView x:Name="myListView" Margin="20" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell IsEnabled="False">
<Grid Margin="0,0,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout VerticalOptions="CenterAndExpand">
<Label Text="{Binding .}" />
</StackLayout>
<local:CustomSwitch x:Name="customSwitch"
SwitchOffColor="Gray"
SwitchOnColor="Red"
SwitchThumbColor="White"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Grid.Column="1"
/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我想要实现的是在切换开关时获取标签文本。在我的ViewModel中,如何在Xamarin表单中实现这一点 最简单的方法是在CustomSwitch中创建一个可绑定的属性
BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(object), null);
public object CommandParameter
{
get { return GetValue(CommandParameterProperty ); }
set
{
SetValue(CommandParameterProperty , value);
}
}
然后在xaml中调整您的声明以
<local:CustomSwitch x:Name="customSwitch"
SwitchOffColor="Gray"
SwitchOnColor="Red"
SwitchThumbColor="White"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Grid.Column="1"
CommandParameter={Binding .}
/>
如何在切换开关时添加要触发的事件处理程序<代码>切换似乎不起作用。请参阅此帖子:
string labelName = (string) ((sender as CustomSwitch).CommandParameter);