问题
In my program, when I hover over (MouseEnter event) a certain UserControl (here I use TransportOrderLineUserControl) a TransportOrderLineDetailView should be shown.
The TransportOrderLineDetailView should be placed at the bottom right of my screen. For this I need to know the ActualWidth & ActualHeight for a correct placement. Because those 2 properties need to have a value different than 0 I execute this code with the DetailView.Loaded event.
I use the exact same way for two other objects. There it works but not for the TransportOrderLineDetailView.
Code where the view is generated:
TransportOrderLineDetailView tolDetailView = new TransportOrderLineDetailView(new TransportOrderLineDetailViewModel(transportOrderLine));
window.DetailContent = tolView;
tolView.Loaded += SetDetailViewOnUI;
Code-behind view:
public TransportOrderLineDetailView(TransportOrderLineDetailViewModel detailViewModel)
{
InitializeComponent();
DataContext = detailViewModel;
}
Method SetDetailViewOnUI:
private void SetDetailPanelOnUI(object sender, EventArgs e)
{
Canvas.SetLeft(window.DetailContentPresenter,
window.grid_Root.ActualWidth - window.DetailContentPresenter.ActualWidth);
Canvas.SetTop(window.DetailContentPresenter,
window.grid_Root.ActualHeight - window.DetailContentPresenter.ActualHeight);
}
Example where it works well:
DriverDetailView driverDetailView = new DriverDetailView(new DriverDetailViewModel(driver));
window.DetailContent = driverDetailView;
driverDetailView.Loaded += SetDetailPanelOnUI;
More information
window.DetailContent:
private UserControl detailContent; public UserControl DetailContent { get { return this.detailContent; } set { this.detailContent = value; OnPropertyChanged("DetailContent"); } }
DetailContentPresenter:
<Canvas Grid.RowSpan="2" x:Name="canvas_Popup_DetailView"> <ContentPresenter Content="{Binding DetailContent}" x:Name="detailContentPresenter"/> </Canvas>
A test where the loaded event also is not triggered:
public TransportOrderLineDetailView(TransportOrderLineDetailViewModel detailViewModel)
{
InitializeComponent();
DataContext = detailViewModel;
this.Loaded += test; //Test here
}
private void test(object sender, System.Windows.RoutedEventArgs e)
{
// method never executed
}
回答1:
Found the solution. There was an error which was not thrown.
How I did find it was going in Visual Studio to Debug -> Exceptions -> Tick the possible exceptions.
Hope this will help other people having trouble with the same stuff. :)
来源:https://stackoverflow.com/questions/21858464/wpf-usercontrol-loaded-event-does-not-trigger