» »

Routed Events WPF [C#]

Routed Events WPF [C#]

Ciklamen ::

Čao

Nikakor ne morm pogruntat kako routed eventi delujejo, še posebej na primeru, ko imaš nek stack panel in na njega šopaš user controle z nekimi informacijami in gumbom in ko pritisneš na ta gumb, se bi morale te informacije oziroma ta objekt (recimo) shranit v listo.

Se pravi kontrola:

<UserControl x:Class="WpfApplication1.Kontrola"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="250" d:DesignWidth="150">
    <Grid>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF551717" Offset="0"/>
                <GradientStop Color="#FFD66565" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Label x:Name="nameL" Content="Name" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <Label x:Name="surnameL" Content="Surname" HorizontalAlignment="Left" Margin="10,30,0,0" VerticalAlignment="Top"/>
        <Label x:Name="ageL" Content="Age" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top"/>
        <Label x:Name="genderL" Content="Gender" HorizontalAlignment="Left" Margin="10,70,0,0" VerticalAlignment="Top"/>
        <Label x:Name="addNameL" HorizontalAlignment="Left" Margin="93,10,0,0" VerticalAlignment="Top"/>
        <Label x:Name="addSurnameL" HorizontalAlignment="Left" Margin="93,30,0,0" VerticalAlignment="Top"/>
        <Label x:Name="addAgeL" HorizontalAlignment="Left" Margin="93,50,0,0" VerticalAlignment="Top"/>
        <Label x:Name="addGenderL" HorizontalAlignment="Left" Margin="93,70,0,0" VerticalAlignment="Top"/>
        <Image x:Name="faceImage" HorizontalAlignment="Left" Height="100" Margin="25,100,0,0" VerticalAlignment="Top" Width="100" Stretch="Fill"/>
        <Button x:Name="addButton" Content="Add" HorizontalAlignment="Left" Margin="37,225,0,0" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Kontrola.xaml
    /// </summary>
    public partial class Kontrola : UserControl
    {

        public static readonly RoutedEvent clickedButtonEvent = 
            EventManager.RegisterRoutedEvent("ButtonClickEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Kontrola));
        public Kontrola()
        {
            InitializeComponent();
        }

        public event RoutedEventHandler Clicked
        {
            add { AddHandler(clickedButtonEvent, value); }
            remove { RemoveHandler(clickedButtonEvent, value); }
        }
    }
}



To je pa še main window

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    
    [XmlRoot("PeopleProfiles")]
    public class PeopleProfiles
    {
        [XmlElement("Person")]
        public List<Person> persons { get; set; }
    };
    public class Person {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("Surname")]
        public string Surname { get; set; }
        [XmlElement("Age")]
        public int Age { get; set; }
        [XmlElement("Gender")]
        public string Gender { get; set; }
        [XmlElement("Image")]
        public string Image { get; set; }
    };
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadAllPersons("All");
        }

        public void LoadAllPersons(string info)
        {
            personWrap.Children.Clear();
            XmlSerializer serializer = new XmlSerializer(typeof(PeopleProfiles));

            using (FileStream stream = File.Open("people.xml", FileMode.Open))
           {
                PeopleProfiles pp = (PeopleProfiles)serializer.Deserialize(stream);

                foreach (Person p in pp.persons)
                {
                    if(info=="All") { 
                        Kontrola k = new Kontrola();
                        k.addNameL.Content = p.Name;
                        k.addSurnameL.Content = p.Surname;
                        k.addAgeL.Content = p.Age;
                        k.addGenderL.Content = p.Gender;
                        k.faceImage.Source = new BitmapImage(new Uri(p.Image, UriKind.Relative));

                        personWrap.Children.Add(k);
                    }
                    else
                    {
                        if (p.Gender == info) {
                            Kontrola k = new Kontrola();
                            k.addNameL.Content = p.Name;
                            k.addSurnameL.Content = p.Surname;
                            k.addAgeL.Content = p.Age;
                            k.addGenderL.Content = p.Gender;
                            k.faceImage.Source = new BitmapImage(new Uri(p.Image, UriKind.Relative));

                            personWrap.Children.Add(k);
                        }
                    }
                }
            };
        }
        public static void DumpException(Exception ex)
        {
            Console.WriteLine("--------- Outer Exception Data ---------");
            WriteExceptionInfo(ex);
            ex = ex.InnerException;
            if (null != ex)
            {
                Console.WriteLine("--------- Inner Exception Data ---------");
                WriteExceptionInfo(ex.InnerException);
                ex = ex.InnerException;
            }
        }
        public static void WriteExceptionInfo(Exception ex)
        {
            Console.WriteLine("Message: {0}", ex.Message);
            Console.WriteLine("Exception Type: {0}", ex.GetType().FullName);
            Console.WriteLine("Source: {0}", ex.Source);
            Console.WriteLine("StrackTrace: {0}", ex.StackTrace);
            Console.WriteLine("TargetSite: {0}", ex.TargetSite);
        }

        private void comboBoxGender_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem box = (ComboBoxItem)comboBoxGender.SelectedItem;
            string item = box.Content.ToString();
            LoadAllPersons(item);
        }

        private void summaryButton_Click(object sender, RoutedEventArgs e)
        {
            summary sum = new summary();
            sum.ShowDialog();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(Kontrola.clickedButtonEvent));
        }
    }
}


Upam da je kolk tolk jasno kaj bi rad, sem pa pregledal že večino linkov na temo routed eventov, ampak nekako ne morem razvozlat zadeve :)
- End of the Post ->


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Visual C# problem

Oddelek: Programiranje
303120 (2292) mladec
»

c# seminarska

Oddelek: Programiranje
112654 (2327) tuned
»

[C#] Deserializacija

Oddelek: Programiranje
181859 (1462) Cvenemir
»

Povezani seznam

Oddelek: Programiranje
132094 (1691) pohano
»

C# tabela

Oddelek: Programiranje
51011 (863) Mitja Bonča

Več podobnih tem