我有一个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);
}
我通过将属性SizeToContent
设置为Manual
而不是WidthAndHeight
并在我的Xaml中指定Width
和Height
来解决这个问题。