【问题标题】:WPF .NET4.0 Listbox and ItemsControlWPF .NET4.0 列表框和ItemsControl
【发布时间】:2011-05-18 02:07:08
【问题描述】:

我试图用项目填充 ListBox。 ItemsSource如下:

public SortedDictionary<string, List<int>> AvailableValues

当我有以下物品时,这些物品似乎布置得很好。除了我无法选择整个项目并对其执行某些功能。

<ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" >
        <ItemsControl ItemsSource="{Binding AvailableValues}">

            <ItemsControl.Template>
                <ControlTemplate>
                    <CustomControls:UniformWrapPanel IsItemsHost="True"/>
                </ControlTemplate>
            </ItemsControl.Template>

            <ItemsControl.ItemTemplate>
                <DataTemplate>

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="60"/>
                        </Grid.ColumnDefinitions>


                        <Label  Grid.Row="0" Grid.Column="0" Content="{Binding Key}" />
                        <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Value}" SelectedItem="{Binding SelectedInputValue, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True" />

                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

我尝试用ListBox(以及ListBox.ItemTemplate)替换ItemsControl,但似乎无法显示Label Content 左对齐和Combobox 内容右对齐。

UniformWrapPanel 来自 CodeProject 文章。

谢谢,

【问题讨论】:

    标签: wpf .net-4.0


    【解决方案1】:

    如果对齐是主要问题,您可能需要尝试将标签和组合框包装到网格中。将水平对齐设置为网格拉伸,并将对象的水平对齐设置为左。

    设置最小最大尺寸是可选的,但可能很有用

    <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                <ColumnDefinition  MinWidth="55" />
                <ColumnDefinition Width="0.636*" />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" HorizontalALignment="Left"/>
                <ComboBox Grid.Column="1" HorizontalAlignment="Left"/>
            </Grid>
    

    【讨论】:

    • 谢谢@randyc。奇怪的是,即使使用上述定义,但将两个项目包含在StackPanel 中,Oriention 设置为Horizontal,它如何失去情节。然而,上面的定义成功了!
    【解决方案2】:

    ItemsControlListBox 不同 - 它不包含选择功能。

    最好的办法是使用实​​际的 ListBox 并修改 ItemTemplate 以显示您想要的内容。

    <ListBox x:Name="MyListBox" ItemsSource="{Binding AvailableValues}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="60"/>
                    </Grid.ColumnDefinitions>
    
                    <Label  Grid.Row="0" Grid.Column="0" Content="{Binding Key}" />
                    <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Value}" />
    
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

    • 干杯@Rachel。即使在ItemsPanel 下设置Canvas,我也花了很多时间。最后@randyc 的建议成功了。
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多