2
我想在C#中编写一个简单的配置文件,它读取XML并相应地自定义组件(按钮,选项卡等)。我的第一个障碍是从变量调用组件,属性和值。这是一个小工作片段,但不是我希望如何工作,因为我没有动态传递组件名称。 (添加到画布虚拟按钮称为btnSample)C#帮助动态改变属性值(Reflection)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
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;
namespace WpfApplication5
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void SetProperty(object target, string propertyName, object value)
{
PropertyInfo property = target.GetType().GetProperty(propertyName);
property.SetValue(target, value, null);
}
private void btnSample_Click(object sender, RoutedEventArgs e)
{
SetProperty(btnSample, "Content", "Clicked");
}
}
}
这不是100%我希望它(以上工作),这里(下不工作)有多远我得和我卡住了,我想叫的名字,都来自变量(源自LINQ/XML)属性和值,任何帮助将是非常有益的:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
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;
namespace WpfApplication5
{
public partial class Window1 : Window
{
string tar;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void SetProperty(object target, string propertyName, object value)
{
PropertyInfo property = target.GetType().GetProperty(propertyName);
property.SetValue(target, value, null);
}
private void btnSample_Click(object sender, RoutedEventArgs e)
{
tar = "btnSample";
SetProperty(tar, "Content", "Clicked");
}
}
}
我想我什么IM确定后解释... 感谢您花时间阅读:)
快速评论你的问题风格 - 如果你发布的代码片段只有相关位,你会得到更多的人真正阅读你的问题(因此更好的答案)。看到像这样的整个班级让人们感到恐惧。 – 2009-09-25 12:43:05