Suppose I have a block, in which all banding is tied to the parameters of a single object. Does it make sense (in terms of performance) to bring it to the top level in the DataContext or is there no significant difference?

For example, with DataContext :

 <StackPanel mui:Switch.When="{x:Static t:ServerStatus.Error}" DataContext="{Binding Entry}"> <!-- server's name --> <TextBlock Text="{Binding DisplayName}" /> <!-- ip, ping --> <TextBox> <TextBox.Text> <MultiBinding StringFormat="{}{0}:{1}"> <Binding Path="Ip" Mode="OneWay" /> <Binding Path="PortHttp" Mode="OneWay" /> </MultiBinding> </TextBox.Text> </TextBox> <mui:BbCodeBlock BbCode="{Binding ErrorsString}" /> <Button Command="{Binding RefreshCommand}" Content="{x:Static c:ControlsStrings.Common_TryAgain}" /> </StackPanel> 

And without:

 <StackPanel mui:Switch.When="{x:Static t:ServerStatus.Error}"> <!-- server's name --> <TextBlock Text="{Binding Entry.DisplayName}" /> <!-- ip, ping --> <TextBox> <TextBox.Text> <MultiBinding StringFormat="{}{0}:{1}"> <Binding Path="Entry.Ip" Mode="OneWay" /> <Binding Path="Entry.PortHttp" Mode="OneWay" /> </MultiBinding> </TextBox.Text> </TextBox> <mui:BbCodeBlock BbCode="{Binding Entry.ErrorsString}" /> <Button Command="{Binding Entry.RefreshCommand}" Content="{x:Static c:ControlsStrings.Common_TryAgain}" /> </StackPanel> 

And then I have some miracles here because of AddLogicalChild along with this very DataContext , I can’t even imagine where to dig.

  • one
    And what, you have a performance problem? - VladD
  • And where did you get the AddLogicalChild ? o_O - VladD
  • @VladD, everything accumulates over time, the interface is not simple. So far, there is nothing very annoying (otherwise I would have gotten rid of bindings), but it is still curious which approach is more correct. - Surfin Bird
  • @VladD, AddLogicalChild use in my panel (here you can see one of its properties, mui:Switch.When ) to switch between controls by condition. So at least in XAML-code looks more elegant than all these Visibility={Binding …, Converter=…, ConverterParameter=…} . Well, I want to believe that it works faster. - Surfin Bird

1 answer 1

This is a matter of taste. Performance differences you probably won't notice.

I usually try to set the narrowest DataContext , as in your top example, to avoid possible errors. This is an analogue of the principle that it is worthwhile (if possible, of course) to transfer only the necessary parameters to the function.

  • Thank! Alas, I do not have enough understanding of how Path=<A>.<B>.<C> works, how many listeners there are, whether they are heavy and optimized, but I still can’t dig up this part of WPF. - Surfin Bird
  • @SurfinBird: In theory, there should be a listener for each component, you can't do without it. But what is the unfortunate 100 listeners compared to one big picture? - VladD