我有一个ListBox DataTemplateWPF.我希望一个项目紧贴在左侧ListBox,另一个项目紧靠右侧,但我无法弄清楚如何做到这一点.
到目前为止,我有一个Grid有三列,左边和右边的列有内容,中心是占位符,宽度设置为"*".我哪里错了?
这是代码:
<DataTemplate x:Key="SmallCustomerListItem">
    <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <WrapPanel HorizontalAlignment="Stretch" Margin="0">
            <!--Some content here-->
            <TextBlock Text="{Binding Path=LastName}" TextWrapping="Wrap" FontSize="24"/>
            <TextBlock Text=", " TextWrapping="Wrap" FontSize="24"/>
            <TextBlock Text="{Binding Path=FirstName}" TextWrapping="Wrap" FontSize="24"/>
        </WrapPanel>
        <ListBox ItemsSource="{Binding Path=PhoneNumbers}" Grid.Column="2" d:DesignWidth="100" d:DesignHeight="50"
     Margin="8,0" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False" HorizontalAlignment="Stretch"/>
    </Grid>
</DataTemplate>
我有一个ListBox,它的ItemTemplate绑定到DataTemplate.我的问题是我无法将模板中的元素拉伸到ListBox的整个宽度.
<ListBox x:Name="listPeople" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
    Margin="0,0,0,0" Background="{x:Null}" SelectionMode="Extended" Grid.Row="1" 
    ItemTemplate="{StaticResource PersonViewModel.BrowserDataTemplate}" 
    ItemsSource="{Binding Mode=OneWay, Path=SearchResults}" >
</ListBox>
<DataTemplate x:Key="PersonViewModel.BrowserDataTemplate">
   <ListBoxItem HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
     <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5">
       <Border Opacity=".1" x:Name="itemBorder"  Background="#FF000000"   
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
         CornerRadius="5,5,5,5" MinWidth="100" Height="50"/>
      </Grid>
   </ListBoxItem>
</DataTemplate>
如您所见,我在网格中添加了一个边框以指示模板的宽度.我的目标是看到此边框扩展到列表框的整个宽度.目前它的宽度由其内容或MinWidth决定,这是目前保持可见的唯一内容.
我ListBoxItem在Windows Phone 8应用程序上遇到问题,同时试图让它们伸展到所有宽度ListBox.
我的ListBox:
<ListBox 
      ItemsSource="{Binding Events}" 
      behaviors:ItemClickCommandBehavior.Command="{Binding EventSelectedCommand}"
      ItemTemplate="{StaticResource EventListTemplateSelector}"/>
它的DataTemplates在一个单独的xaml资源文件中:
<DataTemplate x:Key="EventListHeaderTemplate">
    <Border HorizontalAlignment="Stretch">
        <Grid Height="50">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="6*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="{Binding ImageUri}" VerticalAlignment="Center" HorizontalAlignment="Center" Height="30"/>
            <TextBlock Grid.Column="1" Text="{Binding SomeText}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
        </Grid>
    </Border>
</DataTemplate>
我不能让这些物品真正拉伸,我不知道问题出在哪里.我试图设置ItemContainerStyleHorizontalCOntentAlignment ="Stretch",但它不起作用.我已经尝试了许多其他组合,似乎只将Border或Grid宽度设置为常量工作,另一个有效的解决方案是将Border宽度设置为绑定到包含ListBox的ActualWidth,但我想使用Stretch变种如果可以使它工作.
我是WPF的新手.我试图使OrderItemsTmpl模板中的第一列拉伸到可用的最大宽度,但它不起作用,宽度只能达到文本内部的宽度.我不想使用绝对值.怎么解决这个?谢谢
<DataTemplate x:Key="OrderItemsTmpl">
            <Grid Background="Brown" HorizontalAlignment="Stretch" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0" HorizontalAlignment="Stretch">
                    <CheckBox Content="{Binding Path=sItemName}" HorizontalContentAlignment="Stretch" ></CheckBox>
                    <ListBox HorizontalContentAlignment="Stretch"
                        ItemsSource="{Binding Path=aSinglOptns}"
                        Margin="20,0,0,0"
                        ItemTemplate="{StaticResource SinglOptnTmpl}"
                        Style="{StaticResource SheetListStyle}"
                        ItemContainerStyle="{StaticResource ListBoxItemStyle}"
                 >
                    </ListBox>
                </StackPanel>
                <TextBlock Grid.Column="1" Text="{Binding Path=fQty}"></TextBlock>
</DataTemplate>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <ContentPresenter></ContentPresenter>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<Style x:Key="SheetListStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Background" Value="Aqua"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <Border
                            CornerRadius="8"
                            BorderThickness="2">
                            <ScrollViewer>
                                <WrapPanel
                                    IsItemsHost="True"
                                    Orientation="Vertical"
                                    HorizontalAlignment="Left"/>
                            </ScrollViewer>
                        </Border> …我正在尝试为列表框项目实现 ToggleButton 控件模板。这将用于用户可以单击列表框项目以显示特定功能的应用程序中。
列表框项模板定义如下:
    <Style x:Key="ExampleListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ToggleButton IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                                  HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">
                        <StackPanel Orientation="Vertical">
                            <TextBlock x:Name="ExampleTitle" Grid.Row="0" Foreground="#333333" 
                                       FontFamily="pack://application:,,,/Resources/Fonts/#Neuropol Regular" 
                                       FontSize="16" Height="26" TextAlignment="Left" HorizontalAlignment="Left" 
                                       VerticalAlignment="Top" Text="{Binding ExampleDisplayName}"
                                       Margin="5"></TextBlock>
                            <TextBlock Grid.Row="1" Foreground="#333333" Margin="5,-5,5,3" HorizontalAlignment="Stretch" 
                                       TextAlignment="Left" FontFamily="Verdana" VerticalAlignment="Top" 
                                       TextWrapping="Wrap" Text="{Binding ExampleDescription}"/>
                        </StackPanel>
                    </ToggleButton>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
并且列表框被定义为
        <ListBox x:Name="_examplesListBox" 
                 SelectionMode="Single"
                 BorderBrush="Transparent"
                 Background="Transparent"
                 ItemsSource="{Binding AllExamples}" 
                 ItemContainerStyle="{StaticResource ExampleListBoxItemStyle}"
                 SelectedItem="{Binding SelectedExample, Mode=TwoWay}"/>
这里我有两个文本块,一个绑定到 ExampleDisplayName,另一个绑定到 ExampleDescription。我试图实现的效果是让第二个文本块(描述)环绕,受可用空间的限制。
这就是我现在得到的:

我想要的是显示示例描述的第二行,以根据列表框的大小进行换行。当应用程序启动时,列表框应自动调整为第一行 + 边距。
有什么建议?
xaml ×4
c# ×3
listbox ×3
wpf ×3
datatemplate ×2
layout ×2
alignment ×1
listboxitem ×1
listview ×1
silverlight ×1
windows-8 ×1