[英]How to programatically add a binding converter to a WPF ListView?
I am having a lot of trouble finding a good example of how to programatically create, fill and style a ListView. 我在查找如何以编程方式创建,填充和样式化ListView的一个很好的示例时遇到了很多麻烦。 Every example I find tends to use a lot of XAML markup and a minimum amount of C# to switch which bit of markup is being run.
我发现的每个例子都倾向于使用大量的XAML标记和最少量的C#来切换正在运行的标记位。 This is impossible for me as I do not know the composition of the columns, nor the intended styles, at compile time.
这对我来说是不可能的,因为在编译时我不知道列的组成,也不知道预期的样式。
What I need is pretty trivial - a ListView where a particular cell for a given row will be red if the value is negative, or black of the value is positive. 我需要的是非常简单的 - 一个ListView,如果值为负,则给定行的特定单元格将为红色,或者该值为黑色。 However, which row this is and what colours it will be are not known until runtime (an example that is dynamic/programatic will be sufficient).
但是,这是哪一行以及它将是什么颜色直到运行时才知道(一个动态/程序的例子就足够了)。
What I have (simplified) is something like this: 我所拥有的(简化)是这样的:
string[] columns = new string[] { "Test", "Test2" };
ListView lv = new ListView();
/* Add Columns (works */
GridView viewLayout = new GridView();
foreach (string colName in columns)
{
viewLayout.Columns.Add(new GridViewColumn{ Header = colName });
}
lv.View = viewLayout;
/* Add Items (happy to go the .source path if it's easier) */
foreach (object d in GetData())
{
lv.Items.Add(d);
}
/* Example style, fails */
lv.ItemContainerStyle.Setters.Add(
new Setter(Control.BackgroundProperty, *how do you connect the IValueConverter*)
);
使用Binding,设置Converter属性;)
new Binding() { Converter = new MyAwesomeConverter() }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.