自定义用户控件-绑定或代码隐藏 [英] Custom Usercontrol - Bindings or Codebehind

查看:59
本文介绍了自定义用户控件-绑定或代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


关于自定义用户控件,我只是想知道通过在xaml中定义绑定来设置值是否更好:

Hi
Regarding custom usercontrols I was just wondering whether it is better to set values by defining bindings in the xaml such as:

Visibility="{Binding Path=Show, Converter={cnv:BoolToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ctl:MyControl}}}"



或在后面的代码中设置值,例如:



Or to set the values in the codebehind, such as:

public bool Show
{
     get { return (bool)GetValue(ShowProperty); }
     set { SetValue(ShowProperty, value); }
}

public static readonly DependencyProperty ShowProperty = DependencyProperty.Register
( "Show", typeof(bool), typeof(MyControl), new PropertyMetadata(OnShowChanged));

private static void OnShowChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
  //Pseudo
  theControl.Visibility = (bool)e.NewValue;
}



还是个人喜好下降?

感谢



Or is it down the personal preference?

Thanks

推荐答案

通常,最好将代码放在XAML中.一些纯粹主义者指出,没有代码应包含在后面的代码中,但我不是其中之一.我的态度是,如果严格地与视图相关,那么将代码包含在代码中就可以了,但是我会尽一切努力在xaml中进行操作.在xaml中,有些事情是无法完成的.因此,如果要让大多数人满意,请将其放入xaml中.我怀疑它在xaml中也将更有效,但是不确定.
Generally it is considered better to put the code in the XAML. There are some purists that state that no code should go into the code behind, but I am not one of those. My attitude is that if it is strictly view related, it is fine to have code in the code behind, but I do try to do everything I can in the xaml. There are just some things that cannot be done in the xaml. So, if you want to keep the most people happy, put it in the xaml. I suspect that it will also be more efficient in the xaml, but don''t know for sure.


这篇关于自定义用户控件-绑定或代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆