专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Silverlight

WPF 中ControlTemplate.FindName 偶然访问不到控件

发布时间:2011-06-27 19:24:26 文章来源:www.iduyao.cn 采编人员:星星草
WPF 中ControlTemplate.FindName 偶尔访问不到控件
被一个问题困扰了好几天了,无奈上网求教。
简洁点说明就是 给一个用户控件添加了一个样式文件,访问样式文件中的控件,有时候会访问不到

用户控件cs文件

public class ShowBaseView : UserControl, IDisposable
    {
        ResourceDictionary style1;
        
        public ShowBaseView()
        {
            style1 = new ResourceDictionary();
            style1.Source = new Uri("WBCP;component/Views/BaseUserControlStyle.xaml", UriKind.Relative);
            InitializeStyle();
            this.Loaded += delegate
            {
                InitializeEvent();
            };
            //this.Closing += new System.ComponentModel.CancelEventHandler(BaseWindow_Closing);
        }

        private void InitializeStyle()
        {
            this.Style = (System.Windows.Style)style1["BaseWindowStyle"];
        }

        private void InitializeEvent()
        {
            //this.Style = (System.Windows.Style)style1["BaseWindowStyle"];
            ControlTemplate baseWindowTemplate1 = (ControlTemplate)style1["BaseWindowControlTemplate"];

            Button maxBtn = (Button)baseWindowTemplate1.FindName("btnContMax", this);
            if (maxBtn != null)
                maxBtn.Click += delegate
                {
                    btnContMaxClick(maxBtn);
                };
            else
            {
                MessageBox.Show("加载界面异常!");//有时候会走到这一步,实际上此时该样式文件中的任何控件通过这种方式都访问不到
                return;
            }
           
        }
      /// <summary>
        /// 窗体最大化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void btnContMaxClick(Button btn)
        {
        }

}


样式文件BaseUserControlStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--Window模板-->
    <ControlTemplate x:Key="BaseWindowControlTemplate" TargetType="{x:Type UserControl}">
        <ControlTemplate.Resources>
            <Storyboard x:Key="loadStory">
                <DoubleAnimation Storyboard.TargetName="canvLoading"
                        Storyboard.TargetProperty="RenderTransform.ScaleX" From="0" To="1.0" Duration="0:0:1" AutoReverse="False" RepeatBehavior="1x" />
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"   Duration="00:00:01" RepeatBehavior="1x"  Storyboard.TargetName="canvLoading" Storyboard.TargetProperty="RenderTransform.ScaleY">
                    <SplineDoubleKeyFrame   Value="0"/>
                    <SplineDoubleKeyFrame    Value="-0.5"/>
                    <SplineDoubleKeyFrame    Value="1"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
           
        </ControlTemplate.Resources>
        <Grid Name="WindowGrid" RenderTransformOrigin="0.5,0.5">
            <Grid.RenderTransform>
                <ScaleTransform/>
            </Grid.RenderTransform>
                <!--外边框-->
                <Border Width="Auto" Margin="5"
                    Height="Auto"
                    CornerRadius="8,8,8,8"
                    x:Name="borderTitle" 
                        Background="{StaticResource HeaderBrush}" Effect="{StaticResource ShadowWindow}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="30"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        
                        <Button Height="25" HorizontalAlignment="Right" Margin="0,6,39,0" Name="btnContMax" VerticalAlignment="Top" Width="25" Style="{StaticResource btnTranStyle}" BorderBrush="Transparent" ToolTip="最大化" Grid.RowSpan="2">
                            <Button.Background>
                                <ImageBrush ImageSource="/WBCP;component/images/down_max.png" />
                            </Button.Background>
                        </Button>
                        <!--内容-->                   
                        <Grid Grid.Row="1" Margin="8,5,8,10">
                        <Border  BorderBrush="#1946A1" BorderThickness="1,0,1,1" Name="adContent">
                            <AdornerDecorator>
                                <ContentPresenter />
                            </AdornerDecorator>
                        </Border>
                    </Grid>                  
                </Grid>                    
                </Border>
        </Grid>
    </ControlTemplate>
    <Style x:Key="BaseWindowStyle"
           TargetType="{x:Type UserControl}">
        <Setter Property="MinHeight" Value="100"/>
        <Setter Property="MinWidth" Value="150"/>
        <Setter Property="Template"
                Value="{StaticResource BaseWindowControlTemplate}"/>
    </Style>
</ResourceDictionary>

实际还有子控件之类,要复杂一点。不是每次都访问不到,但是一获取到null值就要命。
大家给点思路......
------解决思路----------------------
可以用下面的方法去获取!
http://blog.csdn.net/rui_china/article/details/10405279
------解决思路----------------------
引用:
Quote: 引用:

可以用下面的方法去获取!
http://blog.csdn.net/rui_china/article/details/10405279

不是子控件,是样式中的控件。这样获取不到,无论如何要把样式挂上吧

看的挺晕的,能不能把样式和模版分开
------解决思路----------------------
引用:
有时候

“有时候”这叫人如何测试好。。。。。。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: