[英]WPF: Bind to datacontext in modal parent window
我正在使用以下方式打開模式窗口:
public void PropertiesTablesButtonClicked(object sender, RoutedEventArgs e)
{
Window _childWindow = new PropertiesTablesWindow();
// Assign MainWindow as the owner of this window, this will cause the MainWindow
// to become inactive and make the child window flash if the main window is clicked
_childWindow.Owner = App.Current.MainWindow;
_childWindow.ShowDialog();
}
從PropertiesTablesWindow.xaml中可以綁定到主窗口的DataContext嗎? 主窗口DataContext具有屬性EditMode,它使我知道程序是否處於編輯模式,該模式又將使子窗口上的DataGrid變為只讀或可編輯狀態,如下所示:
<DataGrid Name="PropertiesDataGrid"
ItemsSource="{Binding PropertiesDataView, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
CanUserAddRows="False"
MaxHeight="200"
IsReadOnly="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Application}}, Path=DataContext.EditMode,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource NegatedStringComparisonToBooleanConverter}, ConverterParameter=Admin}">
我已經嘗試了窗口和應用程序的AncestorType,但顯然這些不起作用。
不同的窗口具有不同的視覺樹。 這就是為什么綁定失敗。 但是,為什么不在所有者窗口的數據上下文中設置模式窗口的數據上下文呢? 會成功的。 當然,您也可以構建一個中介程序來存儲上下文,但是第一個解決方案是如此誘人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.