Friday, September 21, 2012

DataBinding in WrapPanel ?

DataBinding in WrapPanel ?

In a WrapPanel if controls are hard-coded in xaml then it is aligned according to spaces available in the panel . that takes no extra efforts to align the controls.
But how to achieve the same behaviour if those controls are dynamically generated i.e., come through data binding ?
e.g.,
<wrapPanel>
  <checkbox ....>
  <checkbox ....>
  <checkbox ....>
  <checkbox ....>
  <checkbox ....>
  <checkbox ....>
<wrapPanel>

For above code it is aligned according to the spaces available . That's fine .
But how to achieve the same layout if these checkbox are generated through a DataTemplate to populate from binding .

Answers & Comments...

Answer: 1

This will more clearly explain my intension :

<controls:WrapPanel Width="200" >                  <ItemsControl ItemsSource="{Binding Info}" >                      <ItemsControl.ItemTemplate>                          <DataTemplate>                              <TextBlock Text="{Binding Name}" Width="50" HorizontalAlignment="Left"></TextBlock>                          </DataTemplate>                      </ItemsControl.ItemTemplate>                  </ItemsControl>  </controls:WrapPanel>  



All the controls are stacked vertically though there are plenty of spaces available . This works fine if binding is not used and static controls are written in the markup . But what is happening if DataBinding is done ?

Can anyone help please ?



Answer: 2

Any resolution team members ?



Answer: 3

Is it not possible ?



Answer: 4
Yes, you need to set the ItemsPanel to a WrapPanel like so:  
 
<Grid x:Name="LayoutRoot" Background="White">          <Grid.Resources>              <ItemsPanelTemplate x:Name="WrapItems">                  <tk:WrapPanel />              </ItemsPanelTemplate>          </Grid.Resources>            <ItemsControl ItemsSource="{Binding Info}" ItemsPanel="{StaticResource WrapItems}" >              <ItemsControl.ItemTemplate>                  <DataTemplate>                      <TextBlock Text="{Binding FullName}" Width="50" HorizontalAlignment="Left"></TextBlock>                  </DataTemplate>              </ItemsControl.ItemTemplate>          </ItemsControl>      </Grid>



Answer: 5

YES! that did. But can you please tell me where i was lacking or what should i study to resolve these types of problems ?

You did it for me , but what is my weak point ? Can you just take a minute to tell me ?





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog