DataTemplate动态构建时不应用窗口样式

时间:2013-02-19 17:51:15

标签: c# wpf styles

我有一个ResourceDictionary,它包含我的应用程序中使用的控件的样式定义。

所有样式都适用于窗口中的控件。

在一个特定的情况下,我需要通过代码动态构建DataTemplate和Grid(在代码后面)。显示我的控件时,所有项目和数据都会正确显示,但似乎未应用样式。当我单击屏幕边框以调整大小时,样式将在此特定时刻应用。

为什么呢?在显示控件时,如何应用Style?

修改 这是我的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);
  }

1 个答案:

答案 0 :(得分:0)

我通过将属性SizeToContent设置为Manual而不是WidthAndHeight并在我的Xaml中指定WidthHeight来解决问题。