开发者

How do I create a WPF ListBox.ItemTemplate with a variable left column and a fixed right column?

My WPF Listbox should have two columns. The one on the right should have a width of say 20 and the left column should 'fill' the rest of the listbox.

Here is the listbox definition:

   <ListBox ItemsSource="{Binding Path=Stuff}">
      <ListBox.ItemTemplate>开发者_如何学运维
        <DataTemplate>
           <DockPanel LastChildFill="True">
             <TextBlock Text="{Binding Path=Count}" DockPanel.Dock="Right" Width="20">
             </TextBlock>
             <TextBlock Text="{Binding Path=Name}">
             </TextBlock>
           </DockPanel>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>

Does anybody have any idea? Any help would be greatly appreciated.


Try updating the ItemContainerStyle as follows:

<ListBox x:Name="listBox">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel LastChildFill="True">
                <TextBlock Text="{Binding Path=Count}" DockPanel.Dock="Right" Width="20">
                </TextBlock>
                <TextBlock Text="{Binding Path=Name}">
                </TextBlock>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜