XAML 我正在寻找一个描述如何编写一个复杂的wpf ItemsTemplate的代码片段

mwyxok5s  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(116)

我正在显示一个带有格式化文本和图形的列表。代码可以工作,但第二个StackPanel没有在屏幕右侧显示Entity. LabelWdth对象。这就是水平方向不起作用。我不知道如何编写正确的模板(混合面板布局的代码片段)。

<ListBox ItemsSource="{Binding Entity.LowerCanvases}" MouseLeftButtonUp="LowerCanvas_MouseLeftButtonUp">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="-7,10,0,0" Orientation="Vertical">
                <Canvas Width="{Binding Entity.ScrWth, RelativeSource={RelativeSource FindAncestor,
                    AncestorType={x:Type UserControl}}}" Height="{Binding Entity.LowerDrawHght,
                    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">

                    <StackPanel Orientation="Horizontal">
                        <ContentControl Content="{Binding LowerDrawing}" />

                        <ContentControl Content="{Binding LowerLabel}" />
                    </StackPanel>
                </Canvas>

                <Canvas Width="{Binding Entity.ScrWth}" Height="30">

                    <ContentControl Content="{Binding LowerFooter}" />
                </Canvas>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>
qoefvg9y

qoefvg9y1#

我找到了一个解决方案。内容控件也是画布的集合,所以我必须用画布的Left和Top附加属性来定位内容控件。

<ListBox ItemsSource="{Binding Entity.LowerCanvases}" MouseLeftButtonUp="LowerCanvas_MouseLeftButtonUp">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Canvas Margin="-7,10,0,0"
                Width="{Binding Entity.ScrWth, RelativeSource={RelativeSource FindAncestor,
                AncestorType={x:Type UserControl}}}" Height="{Binding Entity.LowerDrawHght,
                RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">

                <!-- These two control should be on the same line -->
                <ContentControl Content="{Binding LowerDrawing}" Canvas.Left="0" />
                <ContentControl Content="{Binding LowerLabel}" Canvas.Left="{Binding Entity.DrawingWth,
                    RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>

                <!-- THis control should be on the following line -->
                <ContentControl Content="{Binding LowerFooter}"  Canvas.Top= "{Binding Entity.LowerDrawHght,
                    RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>

                <!--<TextBlock>
                    <Run Text="{Binding Entity.DrawingWth, StringFormat='{}{0:#,0}',
                            RelativeSource={RelativeSource FindAncestor,
                            AncestorType=UserControl}}" FontSize="22"/>
                </TextBlock>-->

            </Canvas>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

相关问题