动态绑定 - 解析属性名的基础上栏 [英] Dynamic binding - resolving property name based on column
问题描述
我的ListView
在同一列中不同单元格(通过使用实现 DataTemplateSelector
)。这是非常简单的例子:
I have ListView
with different cells in same column (achieved by using DataTemplateSelector
). Here is very simplified example:
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<l:ResultsSelector x:Key="result1">
<l:ResultsSelector.TemplateResult>
<DataTemplate>
<TextBlock Text="{Binding Result1}"/> <!-- how to make this dynamic? -->
</DataTemplate>
</l:ResultsSelector.TemplateResult>
...
</l:ResultsSelector>
...
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Result1" CellTemplateSelector="{StaticResource result1}"/>
<!-- would be nice to use same selector for all columns -->
<GridViewColumn Header="Result2" CellTemplateSelector="{StaticResource result2}"/>
<GridViewColumn Header="Result3" CellTemplateSelector="{StaticResource result3}"/>
...
</GridView>
</ListView.View>
</ListView>
查看模型:
private ObservableCollection<Item> _items;
public ObservableCollection<Item> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}
public class Item: NotifyPropertyChangedBase
{
public string Result1 {get; set;}
public string Result2 {get; set;}
public string Result3 {get; set;}
...
}
问题: ResultSelector
是所有结果列(也有其他的列),唯一的区别是绑定属性的名称相同:列结果1这是结果1
, IsResult1
等,对于结果2 - 结果2
, IsResult2
等
Problem: ResultSelector
is the same for all result columns (there are also other columns), the only difference is names of bound properties: for column "Result1" it's Result1
, IsResult1
, etc., for "Result2" - Result2
, IsResult2
, etc.
目标:我想只是一次定义选择(正如我刚才所说,这是非常简化的版本,所以复制/粘贴RESULT1选择定义 N
时间是非常糟糕的选择,特别是如果我以后要改变一些东西)并用它在某种程度上所有列
Aim: I'd like to define selector just once (as I said earlier, this is very simplified version, so copy/paste "result1" selector definition N
times is pretty bad option, especially if I have to change something later) and use it for all columns somehow.
问:有没有办法来组织的动态的约束力。在动态的我的意思是动态解析取决于列属性的名称。
Question: is there a way to organize dynamic binding. Under dynamic I mean dynamically resolved property name depending on column.
转换器? 用户控件
?想法?
推荐答案
请问像
public String Name
{
get { return name; }
set { name = value; OnPropertyChanged("name"); }
}
和
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
是一种可能性?这样,您至少可以生成字符串并添加此作为参数传递给OnPropertyChanged。
be a possibility? This way you could at least generate the string and add this as a parameter to the OnPropertyChanged.
这篇关于动态绑定 - 解析属性名的基础上栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!