自定义工具窗口没有显示它的上下文

时间:2013-10-31 15:44:34

标签: c# wpf xaml mvvm

我正在创建自定义toolWindow并将其添加到我的userControl。 UserControl是自定义dockingWindows和toolWindows的元素。所以在我的mainWindow xaml中我有DataTemplate

<DataTemplate DataType="{x:Type viewModels:PropertyGridToolWindowViewModel}"> <views:PropertyGridToolWindowView/>
</DataTemplate>

我正在设置包含所有toolWindows和DocumentWindows的userControl:

<ContentControl Content="{Binding}" x:Name="CustomContentControl"/>

我的自定义工具窗口xaml代码如下所示:

<Grid>
    <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Dock="Right">
        <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Dock="Top" >
            <Grid>
                <Button Name="SaveBtn" Content="    Save    " IsEnabled="{Binding SaveButtonIsEnabled}" Grid.Column="0" Command="{Binding SaveClickCommand}" CommandParameter="{Binding ElementName=PropertiesUserControl}"/>
                <Button Name="EditBtn" Content="    Edit    " Grid.Column="1" HorizontalAlignment="Stretch" Command="{Binding EditClickCommand}" IsEnabled="{Binding EditButtonIsEnabled}"/>
                <Button Name="CancelBtn" Content="   Cancel   " Grid.Column="2" HorizontalAlignment="Stretch" Command="{Binding CancelClickCommand}" IsEnabled="{Binding CancelButtonIsEnabled}"/>
            </Grid>
        </DockPanel>
        <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Dock="Bottom">
            <shared:PixelSnapper>
                <propgrid:PropertyGrid SelectedObject="{Binding SelectedObject}">
                </propgrid:PropertyGrid>
            </shared:PixelSnapper>
        </DockPanel>
    </DockPanel>


</Grid>

我的toolWindows和documentWindows的customUserControl是:

  <docking:DockSite x:Name="dockSite" viewModels:DockSiteViewModelBehavior.IsManaged="true" CanDocumentWindowsRaft="True" ItemContainerRetentionMode="Wrapped"
                      DocumentItemsSource="{Binding DocumentItems}" DocumentItemContainerStyle="{StaticResource DocumentItemStyle}"
                      ToolItemsSource="{Binding ToolItems}" ToolItemContainerStyle="{StaticResource ToolItemStyle}">
      <docking:DockSite.AutoHideRightContainers>
            <docking:ToolWindowContainer>
                <views:PropertyGridToolWindow CanClose="False" HasOptions="False"/>
            </docking:ToolWindowContainer>
       </docking:DockSite.AutoHideRightContainers>

       <docking:Workspace x:Name="Workspace>
            <docking:TabbedMdiHost />
        </docking:Workspace>
    </docking:DockSite>

正如您所看到的,我在xaml中添加了toolWindow,它以正确的方式显示。但是我需要动态创建这个工具窗口。而且存在问题。当我动态创建相同的toolWindow时,它不是diplaying我的自定义工具窗口的dockpanel。我该如何解决这个问题? 提前致谢

编辑我创建toolWindows的方式

var toolWindow = new PropertyGridToolWindowViewModel();
toolWindow.Title = "Another ToolWindow";
_toolItems.Add(toolWindow);

问题是未创建视图。但我不明白为什么......

1 个答案:

答案 0 :(得分:0)

我删除了这段代码:

<DataTemplate DataType="{x:Type viewModels:PropertyGridToolWindowViewModel}"> 
<views:PropertyGridToolWindowView/>
</DataTemplate>

来自我的mainWindowXaml。然后我决定按照以下方式创建我的窗口:

var toolWindow = new PropertyGridToolWindowView();
_toolItems.Add(toolWindow);

在我的视图的构造函数中,我创建了我的viewModel,它解决了这个问题。