如何设置DataTemplate生成的按钮的方向?

时间:2018-08-17 08:27:12

标签: c# wpf xaml

我附上了准确描述我的问题的图片。我是wpf初学者,也许我只是不知道哪种标签最适合此目的。

我的整个XAML代码中,我尽可能地设置了水平方向,但是无论如何这些按钮都是垂直生成的。

<Window x:Class="EnterEventTextBox.DataView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EnterEventTextBox"
        mc:Ignorable="d" Background="Black"
        Title="DataView" 
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:cal="http://www.caliburnproject.org"
        >
    <Window.Resources>
        <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
            <WrapPanel Orientation="Horizontal" IsItemsHost="True" Background="Turquoise"/>
        </ItemsPanelTemplate>
        <Style x:Key="ItemsControlStyle1" TargetType="{x:Type ItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                                Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ItemsControl ItemsSource="{Binding Shippers}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Style="{DynamicResource ItemsControlStyle1}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type Button}">
                    <WrapPanel Background="Green" Orientation="Horizontal">
                        <ToggleButton Height="50" Width="50" Background="Red" Content="{Binding BtnLabelShipper}" IsChecked="{Binding IsUnrolled}" Margin="0,0,5,5" 
                                cal:Message.Attach="[Event Click] = [Action ShipperIsClicked($dataContext, $this.IsChecked)]">
                        </ToggleButton>

                        <ItemsControl ItemsSource="{Binding Parcels}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate DataType="{x:Type Button}">
  <!-- THIS BUTTONS LOWER I NEED CREATE IN A HORIZONTAL WAY --> 
                                    <Button Height="50" Width="50" Background="Red" Content="{Binding ParcelNumber}" Margin="0,0,5,5"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>

                    </WrapPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

我对其他也可以解决此问题的人的解决方案。

放入插入:

 string data = "text start this is my text end text";
            string startTag = "start";
            string endTag = "end";
            int startIndex = data.IndexOf(startTag)+ startTag.Length;
            Console.WriteLine(data.Substring(startIndex, data.IndexOf(endTag)-startIndex));

并将此模板添加到(内部)ItemsControl:

<ItemsPanelTemplate x:Key="ItemsStackPanelTemplate1">
            <StackPanel Orientation="Horizontal" IsItemsHost="True" Background="Violet"/>
</ItemsPanelTemplate>

您也不必使用资源声明。这也可以:

<ItemsControl ItemsSource="{Binding Parcels}" ItemsPanel="{DynamicResource ItemsStackPanelTemplate1}">