Silverlight 2.0 – Çalışma Anında Otomatik Kontrol Eklenmesi

Silverlight web forma eklemiş olduğumuz button kontrolü yardımı ile sınırsız button kontrolü eklenmesini açıklayan XAML ve C# kodunu vereceğim. Bu örneği kullanarak diğer kontrolleri de ekleyebilmeniz mümkündür.

XAML
<UserControl x:Class="Controls.Page"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Canvas x:Name="myCanvas">
        <Button x:Name="btnIsMy" Content="Oylesine duruyor..." Canvas.Left="10" Canvas.Top="10"/>
        <Button x:Name="btnAnother" Content="Another" Canvas.Left="10" Canvas.Top="30"/>
        <CheckBox x:Name="rushOrder" Content="Rush" Canvas.Left="10" Canvas.Top="50"/>
    </Canvas>
</UserControl>

C#
using System.Windows;
using System.Windows.Controls;

namespace Controls
{
    public partial class Page
    {
        public Page()
        {
            InitializeComponent();
            btnAnother.Click += BtnAnotherClick;
        }

        private double _newButtonPosition = 100.0;

        void BtnAnotherClick(object sender, RoutedEventArgs e)
        {
            var b = new Button {Content = "Ben yaşıyorum"};
            b.SetValue(Canvas.LeftProperty, 10.0);
            b.SetValue(Canvas.TopProperty, _newButtonPosition);
            _newButtonPosition += 30.0;
            b.Width = 100;
            b.Height = 20;
            b.IsEnabled = true;
            b.Click += b_Click;
            myCanvas.Children.Add(b);
        }

        static void b_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn == null) return;
            btn.Content = "Basma...";
            btn.IsEnabled = false;
        }
    }
}

Umarım yararlı olabillr. İyi çalışmalar…

Yorum Gönder

0 Yorumlar

Ad Code

Responsive Advertisement