【问题标题】:WrapPanel with ItemControl带有 ItemsControl 的 WrapPanel
【发布时间】:2020-03-10 23:06:24
【问题描述】:

我通过使用带有棱镜的 TemplateSelector 来动态创建我的用户界面。 我的问题是我希望所有元素都像这样: 元素 1 元素 2 元素 3 ......
元素 10 元素 11 元素 12 .....

但我得到了这个结果: 元素 1 元素 2 要素 3 . . .

我正在使用 WrapPanel,这是我的代码:

    <WrapPanel>
        <ItemsControl ItemsSource="{Binding Controls}"
                  ItemTemplateSelector="{StaticResource IbuttonTemplateSelector}">
        </ItemsControl>
    </WrapPanel>

这是完整的 xaml 代码

<Window x:Class="WpfApp2.Views.MainView"
        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:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:helpers="clr-namespace:WpfApp2.Helpers"
        xmlns:local="clr-namespace:WpfApp2.Views"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">
    <Window.Resources>

        <!--Button tamplate-->
        <DataTemplate x:Key="ButtonTemplate">
            <Button x:Name="OrderButton"
                        FontSize="10"
                        Height="20" Width="80"
                        Content="{Binding Value}"
                    Margin="0">
                </Button>
        </DataTemplate>

        <!--RadioButton tamplate-->
        <DataTemplate x:Key="RadioTemplate">
            <RadioButton GroupName="gal" Foreground="Black" Content="{Binding Value}" Margin="0">
            </RadioButton>
        </DataTemplate>

        <helpers:IbuttonTemplateSelector x:Key="IbuttonTemplateSelector" 
                                         ButtonTemplate="{StaticResource ButtonTemplate}"
                                         RadioTemplate="{StaticResource RadioTemplate}"/>

    </Window.Resources>

    <!--OUR LIST OF SETTINGS TO DISPLAY-->

    <WrapPanel>
        <ItemsControl ItemsSource="{Binding Controls}"
                  ItemTemplateSelector="{StaticResource IbuttonTemplateSelector}">
        </ItemsControl>
    </WrapPanel>
</Window>

【问题讨论】:

    标签: c# .net wpf mvvm


    【解决方案1】:

    您必须覆盖托管项目的ItemsControl 面板。默认是StackPanel:

    <ItemsControl> 
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel />
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    </ItemsControl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多