Raghunandan Ghagarvale
Raghunandan Ghagarvale

Reputation: 147

Auto adjusting datatemplate inside the parent size

say its a listview bound to a collection i fix the listview width to 100

the itemtemplate is some thing like below

<DataTemplate> <Border>
    <TextBlock Foreground="{Binding Path=Color}"
                Text="{Binding Path=Name}"
                TextTrimming="CharacterEllipsis"/>
</Border> </DataTemplate>

i want to be able to

the templates with multiple data

maximum allowed is 3 how do i make the text box trim to a uniformresize itself to a proper size giving space to other textboxes

i don't wanto have c# code written is it possible to achieve using only XAML ?

Upvotes: 0

Views: 130

Answers (2)

Heena
Heena

Reputation: 8634

try this

 <ListView Width="100">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Width="100" Columns="3"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border>
                <TextBlock Foreground="{Binding Path=Color}" Text="{Binding Path=Name}" TextTrimming="CharacterEllipsis"/>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 1

dkozl
dkozl

Reputation: 33364

You can change ItemsPanel to UniformGrid which will split available space evenly between your items. Here's an ItemsControl example

<ItemsControl ItemsSource="{Binding Path=Items}" >
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <UniformGrid Rows="1"/>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <Border>
            <TextBlock 
                Foreground="{Binding Path=Color}"
                Text="{Binding Path=Name}"
                TextTrimming="CharacterEllipsis"/>
         </Border>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 0

Related Questions