列表视图中的数据绑定
我是 XAML/WPF 新手,遇到了这个奇怪的问题:
我有一个列表视图,我为其设置了数据源。 数据源是“CatalogPartRows”的数组列表。 我在代码中创建我的列。 然后,我设置它们的单元格模板(我的一些列包含组合框和复选框)。 我的问题是,我需要调用“CatalogPartRow”类中的一个函数,该函数获取我需要在单元格中设置的字符串。
这是我尝试使用的代码:
// THIS DOES NOT WORK
//
ObjectDataProvider ODP = new ObjectDataProvider();
ODP.MethodName = "PropertyValueAsString";
ODP.MethodParameters.Add(PropertyName);
ODP.ObjectType = typeof(CatalogPartRow);
Binding DataBindingText = new Binding();
DataBindingText.Source = ODP;
// THIS WORKS
//
//String BindingPathText = /*NOXLATE*/"PropertyValues[" + CPR.IndexOf(PropertyName) + /*NOXLATE*/"]";
//Binding DataBindingText = new Binding(BindingPathText);
FrameworkElementFactory TextBlockElement = new FrameworkElementFactory(typeof(TextBlock));
TextBlockElement.SetBinding(TextBlock.TextProperty, DataBindingText);
FrameworkElementFactory PropertyColumnElement = new FrameworkElementFactory(typeof(Grid));
PropertyColumnElement.AppendChild(TextBlockElement);
DataTemplate DT = new DataTemplate();
DT.VisualTree = PropertyColumnElement;
GVC.CellTemplate = DT;
我的方法正确吗?
CPR = CatalogPartRow
GVC = GridViewColumn
谢谢, 拉杰.
Am new to XAML/WPF and have come across this weird issue:
I have a list view to which I set a DataSource. The DataSource is an arraylist of "CatalogPartRows". I create my columns in the code. I then set their cell templates (some of my columns contain combo boxes and check boxes). My problem here is that I need to call a function in the "CatalogPartRow" class which fetches the string that I need to set in a cell.
Here is the code I am trying to use:
// THIS DOES NOT WORK
//
ObjectDataProvider ODP = new ObjectDataProvider();
ODP.MethodName = "PropertyValueAsString";
ODP.MethodParameters.Add(PropertyName);
ODP.ObjectType = typeof(CatalogPartRow);
Binding DataBindingText = new Binding();
DataBindingText.Source = ODP;
// THIS WORKS
//
//String BindingPathText = /*NOXLATE*/"PropertyValues[" + CPR.IndexOf(PropertyName) + /*NOXLATE*/"]";
//Binding DataBindingText = new Binding(BindingPathText);
FrameworkElementFactory TextBlockElement = new FrameworkElementFactory(typeof(TextBlock));
TextBlockElement.SetBinding(TextBlock.TextProperty, DataBindingText);
FrameworkElementFactory PropertyColumnElement = new FrameworkElementFactory(typeof(Grid));
PropertyColumnElement.AppendChild(TextBlockElement);
DataTemplate DT = new DataTemplate();
DT.VisualTree = PropertyColumnElement;
GVC.CellTemplate = DT;
Is my approach correct?
CPR = CatalogPartRow
GVC = GridViewColumn
Thanks,
Raj.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于将方法包装在属性中不起作用(因为需要参数),因此最好的宠物可能是创建一个值转换器。 您可以使用
PropertyName
作为列的绑定,然后在转换器中您可以将其传递给您的方法,并返回该方法的值。Since wrapping the method in a property won't work (since a parameter is required), the best pet is to probably create a value converter. You can use
PropertyName
as the binding for the column, and then in your converter you can pass that to your method, and return the value of the method.