none
如何从Style中获取ControlTemplate? RRS feed

  • 问题

  • app.xaml内容
    ======================
    <Application xmlns="..." xmlns:local="...">
        <Application.Resources>
            <Style x:Name="DialogBoxStyle1" TargetType="localBig SmileialogBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid x:Name="LayoutRoot">
                    ...
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>
    </Application>


    DialogControlTest.xaml.cs
    ================================
    ...
          //从全局资源中获取名为DislogBoxStyle1的Style,成功
          Style style = Application.Current.Resources["DialogBoxStyle1"] as Style;

         //从Style中获取其中定义的template,失败
          ControlTemplate template = style.Setters[0] as ControlTemplate;


    我要如何编码才能达到目的呢?
    2008年10月11日 17:49

答案

  • Code Snippet

                var style = Application.Current.Resources["buttonStyle"] as Style;
                var setter = style.Setters[0] as Setter;
                var template = setter.Value as ControlTemplate;
                MessageBox.Show((template == null).ToString());

     

     

    2008年10月14日 7:46

全部回复

  • Code Snippet

                var style = Application.Current.Resources["buttonStyle"] as Style;
                var setter = style.Setters[0] as Setter;
                var template = setter.Value as ControlTemplate;
                MessageBox.Show((template == null).ToString());

     

     

    2008年10月14日 7:46
  • 谢谢!!

    2008年10月14日 14:59