Hello,
I'm trying to write a simple config in C# that reads XML and customizes the components (Buttons, Tabs ETC) accordingly. My first snag is calling a component, attribute and value from variables. Here is a small working snippet but not how I want it to work as I’m not passing the component name dynamically. (Dummy button added to Canvas called btnSample)
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");
}
}
}
This is not 100% what I want it to (above working), here (below not working) is how far I've got and I'm stuck, I’m trying to call name, attribute and value all from variables (originating from LINQ/XML), any help would be very helpful :)
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");
}
}
}
I think i've explained what im after ok... Thanks for taking time to read :)