Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在UWP中更改列表框项目(Textblock)的背景_C#_Uwp_Background_Listbox_Textblock - Fatal编程技术网

C# 如何在UWP中更改列表框项目(Textblock)的背景

C# 如何在UWP中更改列表框项目(Textblock)的背景,c#,uwp,background,listbox,textblock,C#,Uwp,Background,Listbox,Textblock,当尝试更改作为列表框数据模板一部分的TextBlock的背景时,背景仅围绕文本,而不是整个块 在UWP中,TextBlock没有background属性,因此我将其包装在边框中,并更改了边框的背景,如下所示: <ListBox x:Name="BitsListView" ItemsSource="{x:Bind BitsList, Mode=TwoWay}" Loaded="BitsListView_Loaded" HorizontalAlignment="Left" IsEnab

当尝试更改作为列表框数据模板一部分的TextBlock的背景时,背景仅围绕文本,而不是整个块

在UWP中,TextBlock没有background属性,因此我将其包装在边框中,并更改了边框的背景,如下所示:

<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>
    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);
        }
    }
但结果是:

我想实现的是这样的目标: (不要介意糟糕的油漆工作)

我试图解决这个问题的方法是用边框包装stackpanel,但没有用。
然后我尝试包装datatemplate,但这是不可能的,再往树上爬一点更改背景无法正常工作,显然更改ListBox的背景会绘制整个列表,我只需要完全绘制必须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>
公共密封部分类主页面:第页
{
私有ObservableCollection位列表{get;set;}
公共主页()
{
this.InitializeComponent();
BitsList=新的ObservableCollection();
对于(int i=0;i<10;i++)
{
随机=新随机();
Add(newtest(){content=random.Next(0,9)});
}
}
}

您确定您的解决方案有效吗?因为我尝试在代码中实现它,但它根本不起作用,然后我尝试在沙箱中运行它,并且项目是垂直顺序的,所以我添加了:
,但结果仍然与我最初的帖子相同:@dimakal Yes。我的代码确实对我有用。看见您没有告诉我您在原始帖子中设置了
。请看我的最新回复。请使用ListView控件并在ItemContainerStyle中添加
。问题是它仍然没有完全着色,当鼠标悬停/单击项目时,项目周围有一个灰色边框,如下所示:另外,使用列表视图对我不利,因为当我单击列表中的项目时,它会刷新整个对象列表,而且它看起来不像good@dimakal
问题是它仍然没有完全着色,当悬停/单击项目时,项目周围有一个灰色边框
,这是因为默认ListViewItem的样式设置了
,您只需要设置
Padding=0
,这个问题就会得到解决。见我的最新答复。
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) });
        }
    }
}