Load Xaml Resource : Resource « Windows Presentation Foundation « C# / C Sharp






Load Xaml Resource

Load Xaml Resource
  
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

    <Button Name="MyButton" HorizontalAlignment="Center" Margin="24">Button</Button>

    <Ellipse Width="200" Height="100" Margin="24" Stroke="Red" StrokeThickness="10" />

    <ListBox Width="100" Height="100" Margin="24">
        <ListBoxItem>Sunday</ListBoxItem>
        <ListBoxItem>Monday</ListBoxItem>
        <ListBoxItem>Tuesday</ListBoxItem>
    </ListBox>

</StackPanel>
//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace MyNameSpace.LoadXamlResource
{
    public class LoadXamlResource : Window
    {

        public LoadXamlResource()
        {
            Title = "Load Xaml Resource";

            Uri uri = new Uri("pack://application:,,,/LoadXamlResource.xml");
            Stream stream = Application.GetResourceStream(uri).Stream;
            FrameworkElement el = XamlReader.Load(stream) as FrameworkElement;
            Content = el;

            Button btn = el.FindName("MyButton") as Button;

            if (btn != null)
                btn.Click += ButtonOnClick;
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Console.WriteLine(args.Source.ToString());
        }
    }
}

   
    
  








Related examples in the same category

1.Use outter resource or inner resource
2.Window level resource
3.Button style as Window resourceButton style as Window resource
4.Event Triggers as ResourceEvent Triggers as Resource
5.Reuse Font With ResourcesReuse Font With Resources
6.Define Double value as resourceDefine Double value as resource
7.Add a logical resource to the window's resource dictionaryAdd a logical resource to the window's resource dictionary
8.Font Size ResourcesFont Size Resources
9.Use EnvironmentInfo as ResourceUse EnvironmentInfo as Resource
10.Dynamic Resource: SystemColors.InactiveCaptionBrushKeyDynamic Resource: SystemColors.InactiveCaptionBrushKey
11.Load Resource for Application
12.No Logical ResourcesNo Logical Resources
13.Logical ResourcesLogical Resources
14.Relative pack URI referring to external component for a ResourceRelative pack URI referring to external component for a Resource
15.Find Control Styles with FindResource()Find Control Styles with FindResource()
16.TextGeometry as ResourceTextGeometry as Resource
17.Retrieving assembly manifest resources
18.Get Resource Names from AssemblyGet Resource Names from Assembly
19.Find Resource with FindResourceFind Resource with FindResource
20.Create Collection based Resource for data bindingCreate Collection based Resource for data binding
21.Wrap collection based resource in a CollectionViewSourceWrap collection based resource in a CollectionViewSource
22.Add event handler to StackPanel in StackPanel resourceAdd event handler to StackPanel in StackPanel resource
23.Add Event handler in Panel ResourceAdd Event handler in Panel Resource
24.DataGridView and ResourceDataGridView and Resource
25.Dynamic ResourceDynamic Resource
26.Load Assembly ResourcesLoad Assembly Resources
27.BitmapImage as ResourcesBitmapImage as Resources
28.Cropped image as Resource
29.Use Resources.Add to add static resouce from codeUse Resources.Add to add static resouce from code
30.Event Setter from ResourcesEvent Setter from Resources