开发者

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

开发者 https://www.devze.com 2023-01-13 01:12 出处:网络
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.

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

精彩评论

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