首先,我尝试了这个,但不知道在哪里和如何实现它。
<ListView.Resources>
<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="DarkGray" Background="#FF92C6F9" Padding="2" HorizontalAlignment="Left" Margin="5" Tag="{Binding Value}" Cursor="Hand" MouseUp="Border_MouseUp_1">
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="DarkGray" Background="WhiteSmoke" Padding="2" HorizontalAlignment="Left" Margin="5" Tag="{Binding Value}" Cursor="Hand" MouseUp="Border_MouseUp_1" >
<TextBlock Text="{Binding Name}" Margin="5" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
我尝试在列表视图名称后或在网格部分内,但得到太多的错误。
这是我的xaml代码
<Window x:Class="Lab_ListView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Lab_ListView"
mc:Ignorable="d"
Title="Video Game List" Height="450" Width="800">
<Grid Margin="0,0,10,10">
<Label Content="Game Name" HorizontalAlignment="Left" Margin="40,59,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="GameNameTextBox" HorizontalAlignment="Left" Height="23" Margin="121,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
<Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="121,151,0,0" VerticalAlignment="Top" Width="75" Click="AddBtn_Click"/>
<ListView x:Name="VideoGameListView" HorizontalAlignment="Left" Height="166" Margin="40,203,0,0" VerticalAlignment="Top" Width="447">
<ListView.View>
<GridView>
<GridViewColumn Header="Game Name" DisplayMemberBinding="{Binding GameName}"/>
<GridViewColumn Header="Rating" DisplayMemberBinding="{Binding Rating}"/>
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}"/>
</GridView>
</ListView.View>
</ListView>
<Label Content="Rating" HorizontalAlignment="Left" Margin="40,90,0,0" VerticalAlignment="Top"/>
<Label Content="Price" HorizontalAlignment="Left" Margin="40,121,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="PriceTextBox" HorizontalAlignment="Left" Height="23" Margin="121,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
<TextBox x:Name="RatingTextBox" HorizontalAlignment="Left" Height="23" Margin="121,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
</Grid>
</Window>
这是类Cropped_Info的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab_ListView
{
class Cropped_Info
{
private string m_GameName;
private string m_Rating;
private double m_Price;
public string GameName
{
get
{
return m_GameName;
}
set
{
m_GameName = value;
}
}
public string Rating
{
get
{
return m_Rating;
}
set
{
m_Rating = value;
}
}
public double Price
{
get
{
return m_Price;
}
set
{
m_Price = value;
}
}
public Cropped_Info()
{
GameName = " ";
Rating = " ";
Price = 0.0;
}
}
}
以及向listView添加项目的主xaml.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Lab_ListView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Cropped_Info vg = new Cropped_Info();
int counter = 0;
Random random;
List<Cropped_Info> list = new List<Cropped_Info>();
System.Windows.Threading.DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
random= new Random();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 0);
dispatcherTimer.Start();
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
vg = new Cropped_Info();
vg.GameName = randStr(5) + " " + counter.ToString();
VideoGameListView.Items.Add(vg);
if(counter == 1000)
{
dispatcherTimer.Stop();
for(int i = 0; i < VideoGameListView.Items.Count; i++)
{
var tt = VideoGameListView.Items[i];
}
}
counter++;
}
public string randStr(int len)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()";
return new string(Enumerable.Repeat(chars, len).Select(s => s[random.Next(s.Length)]).ToArray());
}
}
}
我希望添加到列表视图中的每个字符串项都可以是随机颜色,但主要目标是为每个项着色。
1条答案
按热度按时间jecbmhm31#
您可以使用转换器来实现这一点。例如,颜色的红色通道可能取决于“价格”。“价格”越大,颜色可能越红,如下所示:
下面是一个如何做到这一点的例子:
MainWindow.xaml
主窗口. xaml.cs、项目. cs和价格到背景颜色转换器. cs