WPF 用户控件不呈现。 UserControl 使用 DataTemplates 实例化

Posted

技术标签:

【中文标题】WPF 用户控件不呈现。 UserControl 使用 DataTemplates 实例化【英文标题】:WPF UserControl not rendering. UserControl is instantiated using DataTemplates 【发布时间】:2014-02-07 13:18:09 【问题描述】:

我有一个试图通过数据模板显示的用户控件。不幸的是,控件从未显示。这是它的 DataTemplate:

        <DataTemplate x:Key="listViewTemplate">
                <ctrls:SpecialControl/>
        </DataTemplate>

如果我将常规控件放在这些 DataTemplate 标记中,我可以看到它们。但是,我的 SpecialControl 不会显示。这是我的 SpecialControl 的 xaml 文件:

<UserControl x:Class="CustomControls.SpecialControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">

<StackPanel>
    <CheckBox Content="Hello World" />
    <Button >
        <TextBlock Text="Goodbye world"/>
    </Button>
</StackPanel>

</UserControl>

由于某种原因,此控件在运行时是不可见的。如果我将它们直接放入模板中,我会看到它们。我知道我可以这样做,但我想用这个类做一些更复杂的事情,使用数据绑定和自定义行为。我尝试使用 Snoop,我可以在某处看到我的 SpecialControl,其中的 ContentPresenter 不可扩展:没有复选框或按钮的迹象。

编辑:

这是使用 SpecialControl 的视图:我省略了许多我正在使用的模板,因为我不希望它太拥挤。

<UserControl x:Class="Tools.EditorWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
         xmlns:ctrls="clr-namespace:CustomControls"
         xmlns:local="clr-namespace:Tools"
         mc:Ignorable="d"
         x:Name="This">

<UserControl.Resources>

  <!--More templates -->  

         <DataTemplate x:Key="groupBoxTemplate" >  
        <ctrls:SpecialGroupBox Header="Binding Path=Title" Margin="2" DockPanel.Dock="Top">

            <ItemsControl ItemsSource="Binding guiItemsList" ItemTemplateSelector="DynamicResource guiTemplateSelector">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel>

                        <!-- Set up the width of the individual items based on how many   columns we are supposed to have and what the adjusted width of the wrapPanel is. This way the 4th item will be placed on the 2nd row if the ColumnCount is 3. -->
                        <WrapPanel.ItemWidth>
                            <MultiBinding Converter="StaticResource itemWidthConverter">
                                <Binding Path="ColumnCount"/>
                                <Binding Path="ActualWidth" RelativeSource="RelativeSource Self"/>
                            </MultiBinding>
                        </WrapPanel.ItemWidth> 


                    </WrapPanel>
                </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ctrls:SpecialGroupBox>
    </DataTemplate>  

  <DataTemplate x:Key="listViewTemplate">
            <ctrls:SpecialControl/>
    </DataTemplate>     


   <local:GuiTemplateSelector 
     ... <!--More templates -->
   GroupBoxTemplate="StaticResource groupBoxTemplate"
   SpecialTemplate="StaticResource listViewTemplate"
   x:Key="guiTemplateSelector"/>
</UserControl.Resources>
<DockPanel>

    <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
        <StackPanel Name="rootPanel" DockPanel.Dock ="Top" Width="Auto" Height="Auto">

            <ItemsControl ItemsSource="Binding guiItemsList" ItemTemplateSelector=" StaticResource guiTemplateSelector">
            </ItemsControl>
        </StackPanel>
    </ScrollViewer>
</DockPanel>

</UserControl>

如果您想知道 SpecialGroupBox 的作用,它包含其他控件,以某种方式定位它们。在这个窗口中有几个,它们工作。我的 SpecialControl 应该出现在其中一个 SpecialGroupBoxes 中。

【问题讨论】:

请为使用此 SpecialControl 的视图发布 xaml。 好的。我已经添加了视图。 【参考方案1】:

您的 UserControl 是否有相应的 .xaml.cs 文件?这个问题似乎没有发生。

如果您使用的是 Visual Studio,请尝试从您的 UserControl 复制所有 XAML,然后将其从您的项目中删除,添加一个新的 UserControl(与之前的名称相同),然后将您的内容粘贴回 XAML 文件中。这将确保您设置了正确的 .xaml.cs 文件。

见this answer。

【讨论】:

【参考方案2】:

在您的帖子中,您没有提到您是如何使用 listViewTemplate 的。如果您在 listview/listbox 中使用,您可以尝试这样,它将加载用户控件:

<ListView ItemsSource="Binding Items">
            <ListView.ItemTemplate>
                <DataTemplate >
                    <ContentControl ContentTemplate="StaticResource listviewDataTemplate" Content="Binding"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

【讨论】:

很抱歉给您带来了困惑,但我什至没有在这里使用任何 Listviews,这只是我用作示例的名称。我这里只有一个复选框和一个按钮。

以上是关于WPF 用户控件不呈现。 UserControl 使用 DataTemplates 实例化的主要内容,如果未能解决你的问题,请参考以下文章

“UserControl”类型不支持直接内容

wpf usercontrol,将按钮的命令参数绑定到父用户控件

UserControl在wpf中使用父元素?

当我将WPF UserControl用于其内置项目以外的项目时,我的WPF UserControl无法找到资源

在 UserControl WPF MVVM caliburn 内的 UserControl 之间切换

WPF ----在UserControl的xaml里绑定依赖属性