【发布时间】:2014-10-17 09:48:30
【问题描述】:
我是 Windows Phone 8.1 的新开发人员,我正在尝试从 ListView 集合中获取特定的 ListView 项目,并能够为其着色或为其内部的 TextBock 着色,但我无法到达该项目或到达ListView 中的任何项目,请查看我的以下代码:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
SQLiteRT db1 = new SQLiteRT();
var db_connection = await db1.Connection("MyDB.sqlite");
List<MyTBL> t_list = db1.GetTable("SELECT * FROM MyTBL LIMIT 4 ORDER BY RANDOM() ;");
db_connection.Close();
LV_Options.ItemsSource = t_list;
}
// my List View called LV_Options
private void LV_Options_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView lv1 = sender as ListView;
if (lv1 == null)
return;
MyTBL wrd = lv1.SelectedItem as MyTBL;
if (wrd == null)
return;
TextBlock tb = lv1.FindName("TB_AMean1") as TextBlock;
tb.FontSize = 17; // here I got debug error (it not worked !!!!!!!)
var item = LV_Options.Items.ElementAt(3); // this seems not work also !!!!
item.BackColor = Color.LightSteelBlue;
}
正如您在上面看到的,我尝试通过 LV_Options.Items.ElementAt(3) 到达特定项目,但它不起作用!我也尝试从选定的列表视图项中访问 TextBlock,但也没有成功!
(更新) XAML 代码:
<!-- Title Panel -->
<StackPanel Grid.Row="0" Margin="19,0,0,0">
<TextBlock Name="TB_Rslt" Text="Here result of your answer" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/>
<TextBlock Text="page title" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/>
</StackPanel>
<!--TODO: Content should be placed within the following grid-->
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,10,19,15">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Name="TB_Question" Text="Choose Answer " Margin="0,0,25,0" HorizontalAlignment="Right" FontWeight="Bold" FontSize="22" FontFamily="Verdana" RenderTransformOrigin="0.5,0.5" />
<TextBlock Name="TB_EnWord" Text="" Margin="90,0,15,0" HorizontalAlignment="Left" FontWeight="Bold" FontSize="22" FontFamily="Verdana" RenderTransformOrigin="0.5,0.5" TextAlignment="Right" />
<StackPanel Grid.Row="1" Margin="5,22,0,0">
<ListView Name="LV_Options" SelectionChanged="LV_Options_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="6">
<StackPanel VerticalAlignment="Top" Margin="10,0,0,0">
<TextBlock Name="TB_AMean1" Text="{Binding AMean1}" TextWrapping="Wrap"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
<Button Name="Btn_Answer" Content="Ansewr" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Bottom" Click="Btn_Answer_Click"/>
我的应用程序是一个测验应用程序,它为每个问题提供 4 个选项/选项作为答案,当用户选择一个真实答案时,我想通过将其背景设为绿色来突出显示真实答案(真实选择),如果用户选择了错误的答案/选项我想将该答案(特定列表视图项)的背景设为红色。
有什么帮助吗?
【问题讨论】:
-
您需要使用您想要更改的属性使用 Observable Collection 填充您的 ItemsSource。除非您引发 INofityEvent,否则它不会改变。
-
请参阅stackoverflow.com/questions/25070203/… 了解 Observable Collection 和背景突出显示的示例。
-
我想给正确的选项上色,而不仅仅是选中的选项!
-
@BasharAbuShaman 嗯,您可以修改该代码以完全按照您的意愿行事。我只建议你看看它,这样你就可以理解为什么你的 UI 没有更新。在您的代码中,您需要通知 ListView 项目已更改。这不是使用 OnPaint - Refresh() 进行常规 win32/winforms 编程,您必须遵守他们的规则。
标签: c# xaml windows-phone-8 windows-phone-8.1