I have a ResourceDictionary that contains Style definitions for controls used within my application.
All of the styles are applied properly to the controls in the window.
In one specific case, I need to build DataTemplate and Grid dynamically by code (in code behind). When my control is display, all items and data are correctly displayed but, it's seems that Style are not applied. When I clicked on border of my screen to resize, the Style is applied at this specific moment.
Why? And what can I do to applied Style when control is displayed?
EDIT Here's my Xaml:
<Common:MyPopUpWindow
...
...
>
<Common:MyPopUpWindow.DataContext>
<Controls:DynamicPicklistControlViewModel/>
</Common:MyPopUpWindow.DataContext>
<Grid x:Name="LayoutRoot" Margin="2" Behaviours:ToolbakKeyBehaviour.IsEnabled="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid x:Name="grdPicklist">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Common:MyBlockListBox x:Name="lbxPicklist"
Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="99" />
</Grid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
...
...
</StackPanel>
</Grid>
</Common:MyPopUpWindow >
And this is my code behind:
private void OnShowPicklist(DynamicPicklistControlViewModel.ShowPicklistMessage obj)
{
if (!IsVisible)
{
CreateDataTemplate(obj.ColumnSpecs);
CreateGridLayout(obj.ColumnSpecs);
ShowDialog();
}
}
private void CreateDataTemplate(IEnumerable<AdmPicklistColSpec> columnSpecs)
{
var xaml = new StringBuilder();
xaml.AppendLine("<ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
xaml.AppendLine(" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
xaml.AppendLine(" xmlns:Common='clr-namespace:Xxx.Xxxxxxxxxx.Xxx.Xxxx.Client.Common'>");
xaml.AppendLine("");
xaml.AppendLine(" <DataTemplate x:Key='dtplPicklist'>");
xaml.AppendLine(" <Grid x:Name='grdPicklist'>");
xaml.AppendLine(" <Grid.ColumnDefinitions>");
for (var i = 0; i < columnSpecs.Count(); i++)
{
xaml.AppendLine(" <ColumnDefinition Width='*'/>");
}
xaml.AppendLine(" </Grid.ColumnDefinitions>");
xaml.AppendLine(" <Grid.RowDefinitions>");
xaml.AppendLine(" <RowDefinition Height='{StaticResource DefaultListBoxItemHeight}' />");
xaml.AppendLine(" </Grid.RowDefinitions>");
xaml.AppendLine("");
var iCol = 0;
foreach (var colSpec in columnSpecs)
{
xaml.AppendLine(" <Common:MyFormTextBox x:Name='txtCol" + colSpec.DisplaySeqNbr + "' ");
xaml.AppendLine(" Grid.Column='" + iCol + "' ");
xaml.AppendLine(" Text='{Binding Path=Col" + colSpec.DisplaySeqNbr + "}' />");
iCol++;
}
xaml.AppendLine(" </Grid>");
xaml.AppendLine(" </DataTemplate>");
xaml.AppendLine("</ResourceDictionary>");
var xamlParse = (ResourceDictionary)XamlReader.Parse(xaml.ToString());
foreach (var key in xamlParse.Keys)
{
Resources.Add(key, xamlParse[key]);
}
}
private void CreateGridLayout(IEnumerable<AdmPicklistColSpec> columnSpecs)
{
var grid = grdPicklist;
if (_multiplePicklist)
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
// <ColumnDefinition>
for (var i = 0; i < columnSpecs.Count(); i++)
{
var colDef = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
grid.ColumnDefinitions.Add(colDef);
}
// Adding extra column for scrollbar
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = (GridLength)Application.Current.Resources["DefaultListBoxScrollBarWidth"] });
// </ColumnDefinition>
// Defines Labels
var iCol = 0;
foreach (var label in columnSpecs.Select(colSpec => new MyStackLabel { Content = colSpec.LabelTxt }))
{
Grid.SetRow(label, 0);
Grid.SetColumn(label, iCol);
grid.Children.Add(label);
iCol++;
}
// Block list box
lbxPicklist.ItemTemplate = Resources["dtplPicklist"] as DataTemplate;
lbxPicklist.SetBinding(MyBlockListBox.ItemsSourceProperty, new Binding("Items"));
var colSpan = columnSpecs.Count() + 1;
Grid.SetColumnSpan(lbxPicklist, colSpan);
}