本文介绍了如何改变特定行的风格ListView控件在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的窗口一个ListView我正在绑定到一个列表,现在我想改变基于该行的值特定行的风格。比方说,我想这对他们有一定的价值的行背景为灰色。我怎样才能做到这一点?
< ListView的保证金=0 10 0 0
HEIGHT =205
的HorizontalAlignment =中心
VerticalAlignment =顶部
WIDTH =270
NAME =ElevationList>
< ListView.View>
<&GridView的GT;
< GridViewColumn标题=故事
DisplayMemberBinding ={绑定路径=厄尔尼诺}
WIDTH =100/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
解决方案
XAML code:
<窗口x:类=WpfApplication1.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525
的xmlns:sampleApp =CLR的命名空间:WpfApplication1>
< Window.Resources>
< sampleApp:NumberToBGColorConverter X:键=NumberBGConverter/>
< /Window.Resources>
<网格和GT;
< ListView控件的ItemsSource ={结合ListViewItems}>
< ListView.ItemTemplate>
<数据类型的DataTemplate ={X:类型sampleApp:数}>
<网格背景={结合NumberValue,转换器= {StaticResource的NumberBGConverter}}HEIGHT =20WIDTH =250>
< TextBlock的文本={结合NumberValue}粗细=SemiBold/>
< /网格和GT;
< / DataTemplate中>
< /ListView.ItemTemplate>
< /&的ListView GT;
< /网格和GT;
C#code:
公共部分类主窗口:窗口
{
私人列表<数字与GT; m_ListViewItems; 公开名单<数字与GT; ListViewItems
{
{返回m_ListViewItems; }
集合{m_ListViewItems =价值; }
} 公共主窗口()
{
的InitializeComponent();
的DataContext =这一点;
ListViewItems =新的List<数字与GT;();
ListViewItems.Add(新号码(){NumberValue = 1});
ListViewItems.Add(新号码(){NumberValue = 2});
ListViewItems.Add(新号码(){NumberValue = 3});
ListViewItems.Add(新号码(){NumberValue = 4});
}
}公共类号码
{
私人诠释m_NumberValue; 公众诠释NumberValue
{
{返回m_NumberValue; }
集合{m_NumberValue =价值; }
}
}公共类NumberToBGColorConverter:的IValueConverter
{
公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
变种数=(INT)值; 如果(编号%2 == 0)
返回灰色;
其他
回到黄色;
} 公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
抛出新NotImplementedException();
}
}
I have a ListView in my window that I'm binding to a List, Now I want to change the style of specific rows based on the value of that row. Let's say I want the rows that have a certain value in them to have a grey background. How can I do this?
<ListView Margin="0 10 0 0"
Height="205"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Width="270"
Name="ElevationList">
<ListView.View>
<GridView>
<GridViewColumn Header="Story"
DisplayMemberBinding="{Binding Path=El}"
Width="100"/>
</GridView>
</ListView.View>
</ListView>
解决方案
XAML Code:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:sampleApp="clr-namespace:WpfApplication1">
<Window.Resources>
<sampleApp:NumberToBGColorConverter x:Key="NumberBGConverter"/>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding ListViewItems}">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type sampleApp:Number}">
<Grid Background="{Binding NumberValue,Converter={StaticResource NumberBGConverter}}" Height="20" Width="250">
<TextBlock Text="{Binding NumberValue}" FontWeight="SemiBold"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
C# Code:
public partial class MainWindow : Window
{
private List<Number> m_ListViewItems;
public List<Number> ListViewItems
{
get { return m_ListViewItems; }
set { m_ListViewItems = value; }
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
ListViewItems = new List<Number>();
ListViewItems.Add(new Number() { NumberValue = 1 });
ListViewItems.Add(new Number() { NumberValue = 2 });
ListViewItems.Add(new Number() { NumberValue = 3 });
ListViewItems.Add(new Number() { NumberValue = 4 });
}
}
public class Number
{
private int m_NumberValue;
public int NumberValue
{
get { return m_NumberValue; }
set { m_NumberValue = value; }
}
}
public class NumberToBGColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var number = (int)value;
if (number % 2 == 0)
return "Gray";
else
return "Yellow";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于如何改变特定行的风格ListView控件在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!