开发者

Problem in creating ScrollViewer UserControl in Silverlight

开发者 https://www.devze.com 2023-03-02 18:43 出处:网络
I want to create a MyScrollViewer control with customized style. I have created a UserControl for ScrollViewer (we cant able 开发者_运维知识库create a CustomControl because ScrollViewer From System.Wi

I want to create a MyScrollViewer control with customized style. I have created a UserControl for ScrollViewer (we cant able 开发者_运维知识库create a CustomControl because ScrollViewer From System.Windows.Controls is a sealed class), and in UserControls resources I have added an style for ScrollViewer (Custom Style).

In Codebehind I have created a property Content which overrides the Content of base class(i.e. UserControl).

CodeBehind:

 public partial class ScrollViewer : UserControl
    {
        private System.Windows.Controls.ScrollViewer _scrollViewer;
        public ScrollViewer()
        {
            InitializeComponent();
           // DataContext = this;
             _scrollViewer = new System.Windows.Controls.ScrollViewer();
        }

        public new object Content
        {
            get { return _scrollViewer; }
            set
            {
                _scrollViewer.Content = value;
                base.Content = _scrollViewer;
            }
        }
}

But problem with this code is that I am not able to get Controls inside ScrollViewer at runtime. Suppose I have a TextBox inside ScrollViewer, I am not able to get that control. E.g.:

<ScrollViewer x:Name="scrDetail" >
   <StackPanel Margin="6" Grid.Column="1" Grid.Row="0">
         <Label Name="lblLevel" Content="Level" Margin="2"  />
         <TextBox HorizontalAlignment="Stretch" Margin="2" Name="txtLevel"                           IsEnabled="False" Text="System" />
   </StackPanel>
</ScrollViewer>

I am not able to set text at runtime because I am getting txtLevel as null.


Add below code in custom control class and you will be able to find TextBlock from style.

public override void OnApplyTemplate() {
        base.OnApplyTemplate();
         this.textBlock = this.GetTemplateChild("YourTextBlockName") as TextBlock; }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号