Is there any way that one can force databindings to be initialized on controls right after they are created?
My problem is that I've created a own UserControl derived control which must do some time consuming processing before it is shown. More exactly, create thumbnails of video media using the MediaPlayer component of .Net. I'm displaying my control in a custom made MenuItem control.
As it works now, the control gets initialized right before it is displayed (when a select the parent MenuItem), which starts the time consuming work and forcing me to display some kind of "processing item" information until the control has completed the work.
I need to find a way to make the databinding of filenames to execute as soon as the main window is shown instead of right before my control is displayed. Is it possible?
I've created a small app to demonstrate my problem:
Window1.xaml
<Window x:Class="TestBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="MyMenuStyle" TargetType="MenuItem">
<Setter Property="Header" Value="{Binding MenuHeader}"/>
</Style>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Data">
<MenuItem Header="Submenus" ItemsSource="{Binding SubMenus}" ItemContainerStyle="{StaticResource MyMenuStyle}" />
</MenuItem>
</Menu>
</Grid>
</Window>
Window1.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace TestBinding
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new BindingViewModel();
}
}
class BindingViewModel
{
public ObservableCollection<MyMenuItems> SubMenus { get; set; }
public BindingViewModel()
{
SubMenus = new ObservableCollection<MyMenuItems>();
SubMenus.Add(new MyMenuItems("Menu 1"));
SubMenus.Add(new MyMenuItems("Menu 2"));
SubMenus.Add(new MyMenuItems("Menu 3"));
}
}
public class MyMenuItems
{
private string _menuHeader;
public string MenuHeader
{
get
{
return _menuHeader;
}
set
{
_menuHeader = value;
}
}
public MyMenuItems(string header)
{
_menuHeader = header;
}
}
}
If you run this program and set a breakpoint on the line return _menuHeader; you will notice that this executes as you select the parent menu item. I would like the program to complete the bindings of the sub menu items as soon as possible after the main window is shown giving the program some time to process the values given by the binding property.