【问题标题】:Error: Cannot find the Style Property 'Template' on the type 'System.Windows.Controls.Primitives.Popup'错误:在“System.Windows.Controls.Primitives.Popup”类型上找不到样式属性“模板”
【发布时间】:2011-12-05 09:31:37
【问题描述】:

我必须为弹出窗口创建样式。我正在使用 WPF 和 .NET Framework 4。

我有写作风格:

<Style x:Key="PopupBox" TargetType="{x:Type Popup}">   
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Popup}">       
                <Grid>  
                    <Border BorderBrush="Blue" Background="#FFFFFFFF">
                        <Grid>                                
                            <Border Background="AliceBlue"/>  
                            <ContentPresenter ContentSource="Header" /> 
                            <ContentPresenter/>
                            <Border BorderBrush="#FFFFFFFF" Background="#FFBFDBFF"/>
                        </Grid>
                    </Border>
                </Grid>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style> 

我已经从这段代码中删除了一些元素,如网格行和列定义,因为它们并不重要。

所以看来我不能使用&lt;Setter Property="Template"&gt; 因为Popup 控件没有这个属性。我该如何解决这个问题?

非常感谢您的帮助!

【问题讨论】:

    标签: .net wpf xaml .net-4.0 popup


    【解决方案1】:

    由于 Popup 没有任何模板,只有 Child 内容属性,您可以使用其他一些控件(例如 ContentControl)来设置样式和模板:

    <Style x:Key="PopupContentStyle" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Border BorderBrush="Blue" Background="#FFFFFFFF">
                        <Grid>                                
                            <Border Background="AliceBlue"/>  
                            <ContentPresenter/>
                            <Border BorderBrush="#FFFFFFFF" Background="#FFBFDBFF"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
         </Setter>
    </Style>
    

    然后把它放在每个需要它的Popup 中:

    <Popup>
        <ContentControl Style={StaticResource PopupContentStyle}>
           <!-- Some content here -->
        </ContentControl>
    </Popup>
    

    【讨论】:

      【解决方案2】:

      只能为从Control 类继承的控件设置模板,因为控件类公开了模板属性。但是由于 PopUp 是直接从 FrameworkElement 类继承的,所以不能设置它的 Template 属性。作为一种解决方法,您可以像这样设置它的 Child 属性 -

      <Setter Property="Child">
              <Setter.Value>      
                      <Grid>  
                          <Border BorderBrush="Blue" Background="#FFFFFFFF">
                              <Grid>                                
                                  <Border Background="AliceBlue"/>  
                                  <ContentPresenter ContentSource="Header" /> 
                                  <ContentPresenter/>
                                  <Border BorderBrush="#FFFFFFFF" Background="#FFBFDBFF"/>
                              </Grid>
                          </Border>
                      </Grid> 
              </Setter.Value>
      </Setter>
      

      【讨论】:

      • 使用这种方法将无法在 XAML 中设置 Popup 内容,因为它将覆盖 Style 设置。
      • 这就是我猜的问题中 Cassandra 想要的。他/她正在尝试为其弹出窗口设置控件模板,该模板最终会覆盖应用此样式的默认模板。
      • 从模板中ContentPresenter的存在判断,Popup中的内容不能是静态的。如果您在样式中设置Child,如何在不覆盖该样式的情况下将内容添加到Popup 本身?
      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 2012-12-10
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多