C# 动态生成DataTemplate时未应用窗口样式
我有一个ResourceDictionary,其中包含应用程序中使用的控件的样式定义 所有样式都正确应用于窗口中的控件 在一个特定的案例中,我需要通过代码(在代码隐藏中)动态构建DataTemplate和网格。当我的控件为display时,所有项目和数据都正确显示,但似乎没有应用样式。当我点击屏幕的边框来调整大小时,这个样式就是在这个特定的时刻应用的 为什么??当显示控件时,我可以对应用的样式做什么 编辑 这是我的Xaml:C# 动态生成DataTemplate时未应用窗口样式,c#,wpf,styles,C#,Wpf,Styles,我有一个ResourceDictionary,其中包含应用程序中使用的控件的样式定义 所有样式都正确应用于窗口中的控件 在一个特定的案例中,我需要通过代码(在代码隐藏中)动态构建DataTemplate和网格。当我的控件为display时,所有项目和数据都正确显示,但似乎没有应用样式。当我点击屏幕的边框来调整大小时,这个样式就是在这个特定的时刻应用的 为什么??当显示控件时,我可以对应用的样式做什么 编辑 这是我的Xaml: <Common:MyPopUpWindow ... .
<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);
}
showPicklist上的私有void(DynamicPicklistControlViewModel.ShowPicklistMessage obj)
{
如果(!IsVisible)
{
CreateDataTemplate(obj.ColumnSpecs);
CreateGridLayout(对象列规格);
ShowDialog();
}
}
私有void CreateDataTemplate(IEnumerable columnSpecs)
{
var xaml=新的StringBuilder();
xaml.AppendLine(“”);
xaml.AppendLine(“”);
xaml.AppendLine(“”);
xaml.AppendLine(“”);
xaml.AppendLine(“”);
对于(var i=0;inewmystacklabel{Content=colSpec.LabelTxt}))
{
Grid.SetRow(标签,0);
Grid.SetColumn(标签,iCol);
grid.Children.Add(标签);
iCol++;
}
//阻止列表框
lbxPicklist.ItemTemplate=资源[“dtplPicklist”]作为数据模板;
lbxPicklist.SetBinding(MyBlockListBox.ItemsSourceProperty,新绑定(“Items”);
var colSpan=columnSpecs.Count()+1;
SetColumnSpan(lbxPicklist,colSpan);
}
我通过将属性SizeToContent
设置为Manual
而不是WidthAndHeight
解决了这个问题,并在我的Xaml中指定了Width
和Height
。解决方案:不要在代码中创建WPF UI元素。您是否尝试过先将网格添加到窗口(在代码后面的窗口中)然后设置ItemsSource?添加一个重现问题的小示例可能会有所帮助。