Cro*_*ono 5 .net wpf xaml binding
考虑以下过度简化的用户视图模型:
public class GridRowViewModel
{
public object RowData { get; set; }
public int RowHeight { get;set; }
//...
}
public class GridColumnViewModel
{
public PropertyDescriptor DataFieldDescriptor { get; set; }
public int ColumnWidth { get; set; }
//...
}
Run Code Online (Sandbox Code Playgroud)
正如您可能已经猜到的那样,我希望视图模型能够从代码中操纵类似网格的视图,同时保持实际数据类关注点的分离.目标是允许我在"代理"视图模型中引入行着色或多行编辑机制等功能,这些功能将真实的,未知的数据对象公开为简单属性.
在XAML方面,我想使用类似这样的列模板:
<DataTemplate>
<ContentControl>
<TextBox
Text="{Binding
Source=RowData,
Path=(ext:GridCellExtensions.Column.DataFieldDescriptor)}"
/>
</ContentControl>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如您所知,我通过附加属性与列实例联系.那部分有效.然而,我不知道如何告诉绑定引擎使用其声明的,即用型PropertyDescriptor实例而不是尝试解析一个新实例.事实上,我不知道是否可以这样做.
它可以做到吗?如果是这样,怎么样?
另请注意:为了使这篇文章尽可能简单,上面的模型过于简单,但它们的设计方式是出于超出当前问题的原因.因此,提出一种迫使我重新考虑这种设计的解决方法很可能会产生比它解决的问题更多的问题.因此,我真的在寻找XAML级别的解决方案.如果不存在,我会接受这个事实作为答案.
小智 -1
据我了解,您希望绑定的路径是动态的。
这可以通过使用行为绑定路径属性来实现。例如:
<TextBlock >
<i:Interaction.Behaviors>
<behaviors:CustomBindingBehavior PropertyPath="{Binding Path=HeaderPropertyBinding}" IsBinding="{Binding Path=HeaderIsBinding}" />
</i:Interaction.Behaviors>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我认为这篇文章可以帮助你: