<StackLayout BackgroundColor="White">
<ListView x:Name="ListViewMenu" ItemsSource="{Binding Menus}"
HasUnevenRows="True"
BackgroundColor="White"
SeparatorVisibility="None"
VerticalOptions="FillAndExpand"
ItemTapped="Handle_ItemTapped"
ItemSelected="Handle_ItemSelected"
IsGroupingEnabled = "true"
SeparatorColor="White">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout BackgroundColor="LightSkyBlue" HeightRequest="25">
<Label Text="{Binding Key}" FontAttributes="Bold" LineBreakMode="NoWrap" Margin="10,0,0,0">
<Label.FontSize>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android" Value="20"/>
<On Platform="iOS" Value="20"/>
</OnPlatform>
</Label.FontSize>
</Label>
<StackLayout.VerticalOptions>
<OnPlatform x:TypeArguments="LayoutOptions">
<On Platform="Android" Value="Center"/>
<On Platform="iOS" Value="Center"/>
</OnPlatform>
</StackLayout.VerticalOptions>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding article_description}"
FontAttributes="Bold" FontSize="13" Margin="10,5,0,-6" Grid.Row="0" LineBreakMode="NoWrap"/>
<Label Text="{Binding dish_name}"
FontSize="13" Margin="10,0,0,2" Grid.Row="1" Grid.Column="0"/>
<Label Grid.Row="0" Grid.Column="0" x:Name="LabelReserved" Text="{Binding reserved}" IsVisible="false" LineBreakMode="NoWrap"/>
<Switch Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Center" IsEnabled="False" Toggled="SwitchMenu_OnToggled" >
<Switch.Triggers>
<DataTrigger TargetType="Switch" Binding="{Binding Source={x:Reference LabelReserved},
Path=Text.Length}" Value="7">
<Setter Property="IsToggled" Value="true" />
</DataTrigger>
</Switch.Triggers>
</Switch>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
菜单模型,这是我设置菜单的方式:
public class Menu
{
public string day { get; set; }
public string article_description { get; set; }
public string Date { get; set; }
public string dish_name { get; set; }
public string Reserved { get; set; }
public string Meal { get; set; }
};
我如何实施groupHeader:
首先,我将所有菜单都放在名为GetGroupedViewMenu的列表中。
然后我按星期几组织菜单:
var menus1 = GetGroupedViewMenu.Where(a => a.day == day).ToList();
之后我按照article_description订购列表:
var newList = menus1.OrderBy(m => m.article_description).ToList();
然后我实现了groupHeader:
var sorted = from menu in newList
group menu by menu.Meal into menuGroup
select new Grouping<string, Models.Menu>(menuGroup.Key, menuGroup);
Menus = new ObservableCollection<Grouping<string, Models.Menu>>(sorted);
我想要做的是当我点击开关时,我想从列表视图中获取信息和标题(密钥),该视图位于交换机的同一行。我还想将信息输入到后面的代码中的变量中,这样我就可以使用数据将数据插入到数据库中。我怎么能这样做?
答案 0 :(得分:1)
这里有几个解决方案:
最简单但最丑陋的方法,找到switch的父级和BindingContext
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Switch sw = sender as Switch;
ViewCell cell = sw.Parent.Parent as ViewCell;
object obj = cell.BindingContext;
//do the db action
}
SubClass:
public class MySwitch : Switch
{
public static readonly BindableProperty ConfigurationItemProperty =
BindableProperty.Create(propertyName: "ConfigurationItem", returnType: typeof(Item), declaringType: typeof(Switch), defaultValue: null);
public Item ConfigurationItem { //Item is the class contained in your list.
get{ return (Item)GetValue(ConfigurationItemProperty);}
set { SetValue(ConfigurationItemProperty, value); }
}
}
XMAL:
<ViewCell.View>
<StackLayout>
<local:MySwitch ConfigurationItem ="{Binding}" Toggled="Switch_Toggled" />
</StackLayout>
</ViewCell.View>
代码背后:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
MySwitch sw = sender as MySwitch;
Item item = sw.ConfigurationItem;
//do the db action
}
我发现在ClassId
中定义了Element
(BindableProperty),我最初的想法是在viewModel中添加一个索引属性,用ClassId绑定它,我可以把它想出来代码,但没有运气它不起作用,索引总是为空。
<Switch ClassId="{Binding index}" Toggled="Switch_Toggled" />
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Switch sw = sender as Switch;
string index= sw.ClassId;
//do the db action
}