我有一个 ResourceDictionary,其中包含我的应用程序中使用的控件的样式定义。
所有样式都正确应用于窗口中的控件。
在一种特定情况下,我需要通过代码(在代码后面)动态构建 DataTemplate 和 Grid。当我的控件显示时,所有项目和数据都正确显示,但似乎没有应用样式。当我单击屏幕的边框来调整大小时,会在这个特定时刻应用样式。
为什么?显示控件时,我可以对应用样式做些什么?
编辑 这是我的 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 >
这是我背后的代码:
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);
}