MVVM DelegateCommand的一个不理解的地方

DENQH 2013-09-12 08:07:57
public ICommand TexCommand
{
get
{
return new DelegateCommand<TextBox>(tb =>
{
tb.Text="这个问题"; //出现tb 未实例化,也就是说tb=null
});
}
}
//XAML
<my:RibbonButton Command="{Binding TexCommand}" CommandParameter="{Binding ElementName=tb}" Margin="805,39,0,0" Content="添加" Height="22" VerticalAlignment="Top" HorizontalAlignment="Left" Width="67" Style="{DynamicResource Button}" VerticalContentAlignment="Center" />
红色的地方怎么绑定?也就是CommandParameter绑定问题
绑定数据类没有问题,但怎绑定控件对象?
...全文
311 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
DENQH 2013-09-12
  • 打赏
  • 举报
回复
没有什么反不反,只是探索一下而已
bwangel 2013-09-12
  • 打赏
  • 举报
回复
你这个绑定方法,是反其道而行之。本来ViewModel中应该是纯数据逻辑,把界面控件代进去,是违反了MVVM数据驱动的原则。 而且这个绑定也无法实现。因为要实现绑定,必须把红字改为
CommandParameter={Binding Path=tb,RelativeSource={RelativeSource AncestorLevel=1,AncestorType=Window}}" 
然而tb它本身不是public的,二它也是一个字段而非属性。

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧