I am working on One silverlight 5 application.
In that we are using gridspliter control to make 2 main panels flexible. If we try by using textblock inside first panel from xaml and set gridspliter next to that it's working fine.
But while adding the same textblock through code behind TextWrapping of textblock is not working proper.
we have added the same from code behind as we need to add one image with the that and all the data comes dynamically:
Find the code below:
<StackPanel x:Name="stkRelatedEntity" Grid.Column="0" Grid.Row="1">
HyperlinkButton hyltest = new HyperlinkButton();
StackPanel stk = new StackPanel();
stk.Orientation = Orientation.Horizontal;
Image RImage = new Image();
if (relatedEntity.Image != null)
{
RImage.Source = Common.GetBitMap((byte[])relatedEntity.Image);
RImage.Height = 16;
RImage.Width = 16;
}
TextBlock RText = new TextBlock();
RText.Text = relatedEntity.DisplayNameN;
RText.TextWrapping = TextWrapping.Wrap;
RText.MaxWidth = 250;
RText.MinWidth = 10;
stk.Children.Add(RImage);
stk.Children.Add(RText);
hyltest.Content = stk;
stkRelatedEntity.Children.Add(hykInfo);
Is there a reason why this is generated by code? Will it be an alternative to use a ContentControl with an ItemsSource (ObservableCollection of class holding showing text/image/link), and then put an ItemTemplate for item layout and ItemPanel as StackPanel.
It may not be what you're looking for, but I imagine something like this in the XAML:
<ItemsControl ItemsSource="{Binding Path=MyItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<HyperlinkButton Content="{Binding Path=LinkToShow}" />
<TextBlock Text="{Binding Path=TextToShow}" TextWrapping="Wrap" />
<Image Source="{Binding Path=ImageToShow}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And I imagine the model/code where I have an ObservableCollection, where "SomeClass" is whatever holds the links, image and text to show. This collection is bound to the ItemsSource in the ItemsControl.
Not sure if this is something you can reuse in your project, but I concider it to be a cleaner approach to displaying data.