【问题标题】:How to change background color and color of textbloxk content of selected item of a listbox in WP8?如何在WP8中更改列表框所选项目的文本块内容的背景颜色和颜色?
【发布时间】: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 天,但无法获得解决方案。希望你们帮帮我

【问题讨论】:

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


【解决方案1】:

因为你的 ListBox 有一个 selectionchanged 事件,你可以试试这个。最好的解决方案是使用样式和触发器

    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;
    }

【讨论】:

  • private void Listbox_Main_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (Listbox_Main.SelectedItem != null) { myitem = (KeyValuePair)Listbox_Main.SelectedItem; ListBoxItem myitem1 = Listbox_Main.SelectedItem 作为 ListBoxItem; SolidColorBrush 画笔 = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); myitem1.Background = 画笔;转换逻辑(); } }
  • @user3683525 有用吗?你能发布完整的代码吗
  • 您好,我已经尝试了您提供的上述代码,但 myitem 在这里显示空值。
  • 您的列表中有项目吗?
  • 是的,我正在使用单个数据模板获取项目。 ListBoxItem myitem1 = Listbox_Main.SelectedItem 作为 ListBoxItem;在这里调试时,SelectedItem 正在显示项目,但 myitem1 仍然为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多