【发布时间】:2014-07-29 17:58:57
【问题描述】:
我正在开发 Windows phone 8 应用程序。我正在使用列表框动态显示列表,并且在列表框中项目的选择更改时,我希望所选项目背景颜色为蓝色,文本块文本颜色为白色(仅适用于所选项目) 这是我的列表框 xaml 代码
<Canvas x:Name="Canvas_Main" Margin="23,191,27,75" Tap="Canvas_Main_Tap">
<ListBox x:Name="Listbox_Main" Height="534" Width="430" ItemsSource="{Binding}" SelectionChanged="Listbox_Main_SelectionChanged" Tap="Listbox_Main_Tap" Canvas.Left="10" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="5"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas x:Name="cnv_Items" Height="100" Width="200">
<Border Height="100" Width="200" BorderThickness="2" BorderBrush="#FFD0D0D0"/>
<TextBlock x:Name="Tb_Value" Text="{Binding Value}" TextWrapping="NoWrap" Foreground="Black" TextAlignment="Right" FontSize="26" Height="40" Width="195" Canvas.Top="10" Canvas.Left="2"/>
<TextBlock x:Name="Tb_UnitName" Text="{Binding Key}" Foreground="Black" TextAlignment="Right" FontSize="19" Height="40" Width="180" Canvas.Top="55" Canvas.Left="10"/>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Canvas>
这是我的代码:-
private void Listbox_Main_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (Canvas_KeyBoard.Visibility == Visibility.Visible)
OutAnimation();
if (Listbox_Main.SelectedItem != null)
{
myitem = (KeyValuePair<string, string>)Listbox_Main.SelectedItem;
ListBoxItem myitem1 = Listbox_Main.SelectedItem as ListBoxItem;
SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
myitem1.Background = brush;
Conversion_Logic();
}
}
请告诉我该怎么做,我正在处理这个问题 2 天,但无法获得解决方案。希望你们帮帮我
【问题讨论】:
-
您需要更改 SelectedState 的背景和颜色。使用 Blend 来做到这一点,真的很简单 :)
-
可能重复。检查这个问题:stackoverflow.com/questions/23493221/…
标签: c# windows xaml windows-phone-8 listbox