在我的程序中,当我将鼠标悬停在(MouseEnter 事件)某个用户控件(这里我使用 TransportOrderLineUserControl)上时,应该显示一个 TransportOrderLineDetailView。
TransportOrderLineDetailView 应该放在我屏幕的右下角。为此,我需要知道 ActualWidth 和 ActualHeight 才能正确放置。因为这 2 个属性的值需要不同于 0,所以我使用 DetailView.Loaded 事件执行此代码。
我对另外两个对象使用完全相同的方法。它在那里有效,但不适用于 TransportOrderLineDetailView。
生成视图的代码:
TransportOrderLineDetailView tolDetailView = new TransportOrderLineDetailView(new TransportOrderLineDetailViewModel(transportOrderLine));
window.DetailContent = tolView;
tolView.Loaded += SetDetailViewOnUI;
代码隐藏视图:
public TransportOrderLineDetailView(TransportOrderLineDetailViewModel detailViewModel)
{
InitializeComponent();
DataContext = detailViewModel;
}
方法 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);
}
运行良好的示例:
DriverDetailView driverDetailView = new DriverDetailView(new DriverDetailViewModel(driver));
window.DetailContent = driverDetailView;
driverDetailView.Loaded += SetDetailPanelOnUI;
更多信息
窗口.详细内容:
private UserControl detailContent; public UserControl DetailContent { get { return this.detailContent; } set { this.detailContent = value; OnPropertyChanged("DetailContent"); } }
详细内容演示者:
<Canvas Grid.RowSpan="2" x:Name="canvas_Popup_DetailView"> <ContentPresenter Content="{Binding DetailContent}" x:Name="detailContentPresenter"/> </Canvas>
未触发加载事件的测试:
public TransportOrderLineDetailView(TransportOrderLineDetailViewModel detailViewModel)
{
InitializeComponent();
DataContext = detailViewModel;
this.Loaded += test; //Test here
}
private void test(object sender, System.Windows.RoutedEventArgs e)
{
// method never executed
}