在WPF中動(dòng)態(tài)加載XAML中的控件實(shí)例代碼
本文實(shí)例講述了在WPF中動(dòng)態(tài)加載XAML中的控件的方法。分享給大家供大家參考,具體如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Markup;
/*
* 功能:測(cè)試WPF中動(dòng)態(tài)加載XAML中的控件
* 并加到指定的子節(jié)點(diǎn)中。
* 作者:Kagula
* 時(shí)間:2012-09-20
* 環(huán)境:VS2008 .NET FRAMEWORK 3.5
* 參考資料:[1]《Application=Code+Markup 讀書(shū)筆記 19》
* http://space.itpub.net/15123181/viewspace-423015
* [2]《Pack URIs in Windows Presentation Foundation》
* http://technet.microsoft.com/en-US/library/aa970069(v=vs.90)
*/
namespace testXAMLLoad
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
//LoadEmbeddedXaml();
//LoadEmbeddedXaml2();
LoadEmbeddedXaml3();
}
//從字符串中加載
public void LoadEmbeddedXaml()
{
Title = "Load Embedded Xaml";
string strXaml = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" +
" Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'>" +
" From String Object!</Button>";
StringReader strreader = new StringReader(strXaml);
XmlTextReader xmlreader = new XmlTextReader(strreader);
object obj = XamlReader.Load(xmlreader);
grid1.Children.Add((UIElement)obj);
}
//從外部文件中加載 Button控件
public void LoadEmbeddedXaml2()
{
XmlTextReader xmlreader = new XmlTextReader("d:\\a.xaml");
UIElement obj = XamlReader.Load(xmlreader) as UIElement;
grid1.Children.Add((UIElement)obj);
}
//從資源文件中加載
public void LoadEmbeddedXaml3()
{
//Build Action = Resource,Do not Copy,無(wú)相應(yīng)cs文件
Uri uri = new Uri("/LoadXamlResource.xaml",UriKind.Relative);
Stream stream =Application.GetResourceStream(uri).Stream;
//FrameworkElement繼承自UIElement
FrameworkElement obj =XamlReader.Load(stream) as FrameworkElement;
grid1.Children.Add(obj);
}
}
}
xaml文件中的清單
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'> From File Object! </Button>
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- WPF自定義控件和樣式之自定義按鈕(Button)
- WPF如何自定義TabControl控件樣式示例詳解
- 超炫酷的WPF實(shí)現(xiàn)Loading控件效果
- WPF的ListView控件自定義布局用法實(shí)例
- C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放
- WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果
- WPF實(shí)現(xiàn)ScrollViewer滾動(dòng)到指定控件處
- WPF實(shí)現(xiàn)帶全選復(fù)選框的列表控件
- WPF開(kāi)發(fā)技巧之花式控件功能擴(kuò)展詳解
相關(guān)文章
C#實(shí)現(xiàn)字符串進(jìn)制轉(zhuǎn)換方法匯總
這篇文章主要介紹了C#實(shí)現(xiàn)字符串進(jìn)制轉(zhuǎn)換方法匯總,給大家羅列了十幾種機(jī)制轉(zhuǎn)換問(wèn)題,感興趣的朋友跟隨小編一起看看吧2022-11-11
WPF拖動(dòng)DataGrid滾動(dòng)條時(shí)內(nèi)容混亂的解決方法
這篇文章主要介紹了WPF拖動(dòng)DataGrid滾動(dòng)條時(shí)內(nèi)容混亂的解決方法2016-10-10
C#實(shí)現(xiàn)從windows剪貼板獲取內(nèi)容的方法
這篇文章主要介紹了C#實(shí)現(xiàn)從windows剪貼板獲取內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
Winform+.Net6實(shí)現(xiàn)圖片拖拽上傳功能
這篇文章主要為大家詳細(xì)介紹了如何使用WinformPictureBox控件+.Net6 WebApi實(shí)現(xiàn)圖片拖拽上傳功能,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2023-09-09

