如何在WP8中更改列表框中所选项目的文本块内容的背景颜色和颜色?

时间:2014-06-09 08:08:39

标签: c# windows xaml windows-phone-8 listbox

正在使用Windows phone 8应用。我正在使用列表框动态显示列表,并对列表框中项目的选择更改我希望所选项目背景颜色为蓝色,文本块文本颜色为白色(仅适用于所选项目) 这是我的listbox的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天但无法得到解决方案。希望你伙伴帮助我

1 个答案:

答案 0 :(得分:0)

由于您有一个selectionchanged事件对于ListBox,您可以尝试这样做。最佳解决方案是使用样式和触发器

    private void Listbox_Main_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBoxItem myitem = Listbox_Main.SelectedItem as ListBoxItem;
        SolidColorBrush brush =  new SolidColorBrush(Color.FromArgb(255,255,0,0));
        myitem.Background = brush;
    }
相关问题