WPF UserControl DataTemplate后面的代码更改 [英] WPF UserControl DataTemplate change in code behind
问题描述
大家好,
我在UserControlResources中定义了一个DataTemplate。
< UserControl。资源>
< DataTemplate DataType =" {x:Type item:NpListViewItem}">
< listBlockViews:ListItemView Background =" AntiqueWhite" />
< / DataTemplate>
< /UserControl.Resources>
现在我要更改"templatepart" listBlockViews:ListItemView例如to listBlockViews:ListItemView1在运行时
最好的方法是什么?
Michael,..,
您需要在呈现控件之前更改它。
如果您在之后更改它,则不会发生任何事情。
数据触发器将是我的第一选择。
从viewmodel公开属性,这是您的UserControl的datacontext并使用它来切换出使用哪一个。
如果您真的只是设置背景,那么您可以使用数据触发器,使用一些relativesource绑定来访问usercontrol视图模型。
Hi all,
I´ve a DataTemplate defined in a UserControlResources.
<UserControl.Resources> <DataTemplate DataType="{x:Type item:NpListViewItem}"> <listBlockViews:ListItemView Background="AntiqueWhite" /> </DataTemplate> </UserControl.Resources>
Now I want to change the "templatepart" listBlockViews:ListItemView e.g. to listBlockViews:ListItemView1 in runtime
what´s the best way to do this ?
Michael,..,
Hi,
You need to change it before the controls are rendered.
If you change it after then nothing will happen.
A datatrigger would be my first choice.
Expose a public property from the viewmodel which is the datacontext of your UserControl and use that to switch out which one to use.
If you're really only setting the background then you could use a datatrigger on that, with some relativesource binding to get to the usercontrol viewmodel.
这篇关于WPF UserControl DataTemplate后面的代码更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!