0
votes

I have a user control that I`m trying to show via data templates. Unfortunately, the control never shows. Here's the DataTemplate for it:

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

If I put regular controls inside those DataTemplate tags, I can see them. My SpecialControl however, won't show. Here's the xaml file for my SpecialControl:

<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>

For some reason, this control is invisible at run time. If I put them directly into the template, I'll see them. I know I could just do that, but I want to do something more complex with this class, using databinding, and custom behavior. I tried using Snoop, and I can see my SpecialControl in there somewhere, with a ContentPresenter that's not expandable: no sign of The checkbox or the button.

Edit:

Here is the View that is using the SpecialControl: I left out many of the templates I'm using, because I don't want this to be too crowded.

<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>

If you're wondering what the SpecialGroupBox does, it holds other controls, positioning them a certain way. There are a few of them in this window, and they work. It is inside of one of these SpecialGroupBoxes that my SpecialControl is supposed to appear.

2
Post the xaml for the view that is using this SpecialControl please.Lee O.
OK. I've added the View.NickLokarno

2 Answers

1
votes

Does your UserControl have a corresponding .xaml.cs file? This problem seems to happen when it doesn't.

If you're using Visual Studio, try copying all the XAML from your UserControl, then deleting it from your project, adding a new UserControl (with the same name as before), then pasting your content back into the XAML file. This will ensure that you have the correct .xaml.cs file set up.

See this answer.

0
votes

In your post you didn't mention how you are using the listViewTemplate . if your using inside listview/listbox you can try like this it will load the user control:

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