Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Regarding custom usercontrols I was just wondering whether it is better to set values by defining bindings in the xaml such as:

XML
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:

C#
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
Posted
Updated 30-Oct-12 0:00am
v2

1 solution

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.
 
Share this answer
 
Comments
Adam_Dev 30-Oct-12 11:58am    
Thanks Clifford. I have the same attitude, I was just after some clarification really, which you provided.

Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900