Items in ItemsControl not constrained to container with ItemsHost=true

Traitée Items in ItemsControl not constrained to container with ItemsHost=true

  • jeudi 5 avril 2012 18:38
     
      A du code

    I have what I think is a simple control.   For the purposes of this sample I have reduced it to the following

    <ItemsControl x:Class="TestControl.MyItemsControl"
                 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" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid Margin="20,0,20,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
    
            <TextBlock Grid.Row="0" Text="MyText" />
    
            <ItemsControl Grid.Row="1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Grid>
    </ItemsControl>
    

    In the designer this looks right. Text on top.  Items collection on the bottom. Margins on the side. However, when I put it my application the items are not constrained to the margins. The text is just not the items. My application looks like:

    <Window x:Class="TestControl.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:tc="clr-namespace:TestControl"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <tc:MyItemsControl>
                <TextBlock Text="Test1" />
                <TextBlock Text="Test2" />
            </tc:MyItemsControl>
        </Grid>
    </Window>

    The end result looks like:

    So why does the text from the items control not stay within the margins?   Like the TextBox does?


    sjz

Toutes les réponses

  • mercredi 11 avril 2012 09:43
    Modérateur
     
     Traitée

    Hi sjz,

    I don't get why you can create a custom itemscontrol in your way.

    It is seemed that you create a UserControl as custom ItemsControl, but UserControl just can't inherit form ItemsControl. You should need to create a CustomControl to achieve you goal I think. But also you are not create a CustomControl. At the same time, the MyItemsControl you create without any binding action and Denpendency property, no way you can create a worked UserControl. Here is a simple sample about how to create a UserControl within ItemsControl for you to reference:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b9c27238-145b-494f-8d22-8fa1bda336ad

    If I misunderstand you or you have any additional questions, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • lundi 16 avril 2012 19:25
    Modérateur
     
     

    Hi sjz,

    Have hear from you for a week, so I'm temprorarily marking this as answer, if you have any new finds, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us