【发布时间】:2019-10-24 03:10:05
【问题描述】:
我有一个 ListView,其中有一个在 ViewCell 中定义的按钮。我想知道如何以编程方式更新此按钮的颜色。
这就是我的 ListView 在 XAML 中的定义方式。
<ListView x:Name="HeaderListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Title}" TextColor="Black"/>
<Button x:Name="DetailButton" Grid.Row="1"
BackgroundColor="Blue"
TextColor="White"
Text="Details"
Clicked="DetailButton_Clicked"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
数据源是我用C#设置的SQLite数据库:
protected override void OnAppearing()
{
base.OnAppearing();
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<Header>();
var header = conn.Query<Header>("select * from Header");
HeaderListView.ItemsSource = header;
}
}
Detail 按钮选择它所在的行,并导航到使用所选行的 Header 数据构建的 DetailPage。 DetailPage 包含第二个 ListView,这是一个 Detail 表,它通过外键链接到 Header 表。 Detail 表上的每一行都有一个布尔属性Complete。 DetailPage的ListView的ItemsSource的设置方法和HeaderListView一样。
一些示例代码来反映我的表格的样子:
public class Header
{
public int HeaderId { get; set; } // The primary key of the Header table
public string Code { get; set; } // Foreign key of the Detail table
// various Header data
public string Data1 { get; set; }
public DateTime Data2 { get; set; }
public int Data3 { get; set; }
}
public class Detail
{
public int DetailId { get; set; } // Primary key of Detail table
public string Code { get; set; } // Foreign key; Many Detail rows to one Header row
// various Detail data
public string Detail1 { get; set; }
public int Detail2 { get; set; }
public TimeSpan Detail3 { get; set; }
public bool Complete { get; set; }
}
HeaderListView 上我的详细信息按钮的期望行为如下:
1) BackgroundColor="Blue" 当 Header 没有匹配的 Detail 行时,因此 DetailPage ListView 中没有项目。
2) BackgroundColor="Orange" 当 Header 至少有一个匹配的 Detail 行(因此在 DetailPage ListView 中至少有一个项目),并且 any 行的 Complete 属性设置为 @ 987654329@.
3) BackgroundColor="Green" 当 DetailPage 至少有一个匹配的行,并且 所有 它的“行”Complete 属性设置为 true。如果只有一行与 Header 匹配且其 Complete 属性为 true,则 Detail 按钮仍为绿色。
请不要对自定义工具提出建议,除非绝对无法使用默认 Xamarin 工具执行此操作。
【问题讨论】:
标签: c# xaml listview xamarin xamarin.forms