【问题标题】:How to change background of listbox items (Textblock) in UWP如何在 UWP 中更改列表框项目(文本块)的背景
【发布时间】:2019-11-08 13:33:29
【问题描述】:

当尝试更改作为 ListBox 的 DataTemplate 一部分的 TextBlock 的背景时,背景仅围绕文本而不是整个块

在 UWP TextBlock 中没有背景属性,所以我将它包裹在边框中并像这样更改边框的背景:

<ListBox x:Name="BitsListView" ItemsSource="{x:Bind BitsList, Mode=TwoWay}" Loaded="BitsListView_Loaded"
    HorizontalAlignment="Left" IsEnabled="{x:Bind IsWriteAccess,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    SelectionChanged="BitsListView_SelectionChanged " SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border>
                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name="BitText" Text="{Binding}" Loaded="BitText_Loaded" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>

颜色在 OnLoaded 事件中发生变化,如下所示:

    private void BitText_Loaded(object sender, RoutedEventArgs e)
    {
        TextBlock bitText = sender as TextBlock;

        StackPanel sp = bitText.Parent as StackPanel;
        Border border = sp.Parent as Border;

        if ((int)bitText.DataContext == 1)
        {
            bitText.Foreground = new SolidColorBrush(Windows.UI.Colors.LightGreen);
            border.Background = new SolidColorBrush(Windows.UI.Colors.DarkGreen);

        }
        else
        {
            bitText.Foreground = new SolidColorBrush(Windows.UI.Colors.Gray);
            border.Background = new SolidColorBrush(Windows.UI.Colors.LightGray);
        }
    }

但结果是这样的: https://pasteboard.co/IlcZB1J.png

我想要达到的是这样的: (不要介意糟糕的 MSPaint 工作)

https://pasteboard.co/Ild1plp.png

我试图解决这个问题的方法是用边框包裹堆栈面板,但这没有帮助。 然后我尝试包装数据模板,但这是不可能的,进一步爬上树改变背景不能正常工作,显然改变 ListBox 的背景会绘制整个列表,我只需要具有 1 的块完整地绘制,而不仅仅是围绕文本的一点点

【问题讨论】:

    标签: c# uwp background listbox textblock


    【解决方案1】:

    对于您的问题,您不需要使用边框来包裹 StackPanel,它不会起作用。您只需为ListBoxItem 定义一个样式并将其应用于ItemContainerStyle 并设置HorizontalContentAlignment=Stretch

    在这里,我检查了您的代码。我有一些建议给你。在 UWP 中,您可以通过绑定完成大多数事情。这意味着您无需在页面的代码隐藏中从 DataTemplate 中查找特定控件并设置其属性值。这不是最佳做法。相反,您可以定义一个包含三个属性(文本、背景、前景)的自定义类。然后,您可以绑定到 XAML 页面上的这些属性。

    完整的代码示例如下:

    <ListView x:Name="BitsListView" ItemsSource="{x:Bind BitsList, Mode=TwoWay}"
    HorizontalAlignment="Left" SelectionMode="Single">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                    <Setter Property="Padding" Value="0"></Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" ></StackPanel>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Test">
                    <StackPanel Background="{x:Bind backGround}">
                        <TextBlock x:Name="BitText" Text="{x:Bind content}" Foreground="{x:Bind foreGround}" HorizontalTextAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    
    public class Test : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private int _content;
    
        public int content
        {
            get { return _content; }
            set
            {
                if (_content != value)
                {
                    _content = value;
                    if (value == 1)
                    {
                        foreGround = new SolidColorBrush(Colors.LightGreen);
                        backGround = new SolidColorBrush(Colors.DarkGreen);
                    }
                    else
                    {
                        foreGround = new SolidColorBrush(Colors.Gray);
                        backGround = new SolidColorBrush(Colors.LightGray);
                    }
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("content"));
                }
            }
        }
    
        private SolidColorBrush _backGround;
    
        public SolidColorBrush backGround
        {
            get { return _backGround; }
            set
            {
                if (_backGround != value)
                {
                    _backGround = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("backGround"));
                }
            }
        }
    
        private SolidColorBrush _foreGround;
    
        public SolidColorBrush foreGround
        {
            get { return _foreGround; }
            set
            {
                if (_foreGround != value)
                {
                    _foreGround = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("foreGround"));
                }
            }
        }
    }
    
    public sealed partial class MainPage : Page
    {
        private ObservableCollection<Test> BitsList { get; set; }
    
        public MainPage()
        {
            this.InitializeComponent();
            BitsList = new ObservableCollection<Test>();
            for (int i = 0; i < 10; i++)
            {
                Random random = new Random();
                BitsList.Add(new Test() { content = random.Next(0, 9) });
            }
        }
    }
    

    【讨论】:

    • 您确定您的解决方案有效吗?因为我尝试在我的代码中实现它并且它根本不起作用,所以我尝试在沙箱中运行它并且项目是垂直顺序的,所以我添加了:&lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; 但是结果还是和我原来的帖子一样:pasteboard.co/IlkwNc2.png
    • @dimakal 是的。我的代码确实对我有用。见Screenshot。您没有告诉您在原始帖子中设置了&lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt;。请看我更新的回复。请使用 ListView 控件并在 ItemContainerStyle 中添加&lt;Setter Property="VerticalContentAlignment" Value="Stretch"&gt;&lt;/Setter&gt;。见Screenshot
    • 问题是它仍然没有完全着色,当悬停/单击项目时,项目周围有一个灰色边框,如下所示:pasteboard.co/IllI163.png
    • 另外,使用列表视图对我不利,因为当我单击列表中的项目时它会刷新整个对象列表,而且看起来不太好
    • @dimakal The problem is that its still not coloring it fully, when hovering/clicking the item there is a grayish border around the item 因为默认ListViewItem的样式设置了&lt;Setter Property="Padding" Value="12,0,12,0" /&gt;,你只需设置Padding=0,就可以解决这个问题了。查看我的更新回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多