Home > Enterprise >  How to draw horizontal line in my end of list in WPF?
How to draw horizontal line in my end of list in WPF?

Time:09-26

I want a blue line stretch end listitem but only a few come.

I gave

HorizontalAlignment = "Stretch"

but it doesn't work.

Full list code here

 <Grid>
        <ListBox Name="MyListBoxAddData" Margin="20" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Id}"/>
                        <Label Content="{Binding  Path=Questions,Mode=TwoWay}"/>
                        <RadioButton Content="{Binding  Path=Option1,Mode=TwoWay}" Margin="10 5"/>
                        <RadioButton Content="{Binding Path=Option2,Mode=TwoWay}" Margin="10 5"/>
                        <RadioButton Content="{Binding Path=Option3,Mode=TwoWay}" Margin="10 5"/>
                        <RadioButton Content="{Binding Path=Option4,Mode=TwoWay}" Margin="10 5"/>
                        <TextBlock Text="{Binding Answer}" />
                        <Separator HorizontalContentAlignment="Stretch"/>
                        <Rectangle HorizontalAlignment="Stretch"  Fill="Blue" Height="4" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

O/P Draw full Horizontal line

I want a full line. Thank you in Advance

CodePudding user response:

The problem is that the StackPanels width is given by its content. The solution is to set the StackPanels width equals to the ActualWidth of the ListBox. That solves the problem.

 <Grid>
    <ListBox
        Name="MyListBoxAddData"
        Margin="20"
        BorderThickness="0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Width="{Binding RelativeSource={RelativeSource AncestorType=ListBox, Mode=FindAncestor}, Path=ActualWidth}">
                    <Label Content="{Binding Id}" />
                    <Label Content="{Binding Questions}" />
                    <RadioButton Margin="10,5" Content="{Binding Option1}" />
                    <RadioButton Margin="10,5" Content="{Binding Option2}" />
                    <RadioButton Margin="10,5" Content="{Binding Option3}" />
                    <RadioButton Margin="10,5" Content="{Binding Option4}" />
                    <TextBlock Grid.Row="6" Text="" />
                    <Rectangle Height="8" Fill="Blue" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
  • Related