WPF绑定listviewitem的前景色
如何将 ListViewItem 的前景色绑定到模型的属性?
public class UserModel : BaseModel
{
public string UserName { get; private set; }
public int UserID { get; private set; }
public Brush Colour
{
get
{
return m_colour;
}
set
{
if (object.ReferenceEquals(m_colour, value))
return;
m_colour = value;
OnPropertyChanged("Colour");
}
}
private Brush m_colour = Brushes.Black;
public UserModel(int userID, string userName)
{
UserName = userName;
UserID = userID;
}
}
How would i bind the foreground colour of a ListViewItem to a property of a model?
public class UserModel : BaseModel
{
public string UserName { get; private set; }
public int UserID { get; private set; }
public Brush Colour
{
get
{
return m_colour;
}
set
{
if (object.ReferenceEquals(m_colour, value))
return;
m_colour = value;
OnPropertyChanged("Colour");
}
}
private Brush m_colour = Brushes.Black;
public UserModel(int userID, string userName)
{
UserName = userName;
UserID = userID;
}
}
<ListView Name="lvClients" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" ItemsSource="{Binding Users, Mode=OneWay}" DisplayMemberPath="UserName" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何找到颜色属性取决于您的完整结构:
或者您直接绑定到
ListView
的Foreground
,这将导致项目具有相同的前景。How you find your color property depends on your full structure:
Or you just bind to the
Foreground
of theListView
directly which will cause the items to have the same foreground.您应该使用
ItemTemplate
。例如,可以在此处找到有关此内容的更多信息: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx
You should use an
ItemTemplate
. For instanceMore info on this can be found here: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx